diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java index ac70ddf935..cf4fedaeea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java @@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.rsocket; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer; import org.springframework.boot.rsocket.server.RSocketServerBootstrap; import org.springframework.boot.rsocket.server.RSocketServerFactory; import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; -import org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/LocalRSocketServerPort.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/LocalRSocketServerPort.java similarity index 96% rename from spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/LocalRSocketServerPort.java rename to spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/LocalRSocketServerPort.java index a44ce5f06c..7da1f6a32c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/LocalRSocketServerPort.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/LocalRSocketServerPort.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.web.server; +package org.springframework.boot.rsocket.context; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/RSocketPortInfoApplicationContextInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java similarity index 57% rename from spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/RSocketPortInfoApplicationContextInitializer.java rename to spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java index f68271df05..09fb5d05f5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/RSocketPortInfoApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java @@ -14,13 +14,12 @@ * limitations under the License. */ -package org.springframework.boot.web.context; +package org.springframework.boot.rsocket.context; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.rsocket.context.RSocketServerInitializedEvent; import org.springframework.boot.rsocket.server.RSocketServer; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -45,41 +44,52 @@ import org.springframework.core.env.PropertySource; * @since 2.2.0 */ public class RSocketPortInfoApplicationContextInitializer - implements ApplicationContextInitializer, - ApplicationListener { - - private ConfigurableApplicationContext applicationContext; + implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { - applicationContext.addApplicationListener(this); - this.applicationContext = applicationContext; + applicationContext.addApplicationListener(new Listener(applicationContext)); } - @Override - public void onApplicationEvent(RSocketServerInitializedEvent event) { - String propertyName = "local.rsocket.server.port"; - setPortProperty(this.applicationContext, propertyName, event.getrSocketServer().address().getPort()); - } + private static class Listener implements ApplicationListener { - private void setPortProperty(ApplicationContext context, String propertyName, int port) { - if (context instanceof ConfigurableApplicationContext) { - setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), propertyName, port); - } - if (context.getParent() != null) { - setPortProperty(context.getParent(), propertyName, port); - } - } + private static final String PROPERTY_NAME = "local.rsocket.server.port"; - @SuppressWarnings("unchecked") - private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) { - MutablePropertySources sources = environment.getPropertySources(); - PropertySource source = sources.get("server.ports"); - if (source == null) { - source = new MapPropertySource("server.ports", new HashMap<>()); - sources.addFirst(source); + private ConfigurableApplicationContext applicationContext; + + Listener(ConfigurableApplicationContext applicationContext) { + this.applicationContext = applicationContext; } - ((Map) source.getSource()).put(propertyName, port); + + @Override + public void onApplicationEvent(RSocketServerInitializedEvent event) { + setPortProperty(this.applicationContext, event.getrSocketServer().address().getPort()); + } + + private void setPortProperty(ApplicationContext context, int port) { + if (context instanceof ConfigurableApplicationContext) { + setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), port); + } + if (context.getParent() != null) { + setPortProperty(context.getParent(), port); + } + } + + private void setPortProperty(ConfigurableEnvironment environment, int port) { + MutablePropertySources sources = environment.getPropertySources(); + PropertySource source = sources.get("server.ports"); + if (source == null) { + source = new MapPropertySource("server.ports", new HashMap<>()); + sources.addFirst(source); + } + setPortProperty(port, source); + } + + @SuppressWarnings("unchecked") + private void setPortProperty(int port, PropertySource source) { + ((Map) source.getSource()).put(PROPERTY_NAME, port); + } + } } diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories index 2641b68bec..137c661cf9 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories @@ -16,8 +16,8 @@ org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer,\ org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\ -org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer,\ -org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer +org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\ +org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer # Application Listeners org.springframework.context.ApplicationListener=\ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/server/LocalRSocketServerPortTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/context/LocalRSocketServerPortTests.java similarity index 97% rename from spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/server/LocalRSocketServerPortTests.java rename to spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/context/LocalRSocketServerPortTests.java index bde44bd33c..452a7d8e1d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/server/LocalRSocketServerPortTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/context/LocalRSocketServerPortTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.web.server; +package org.springframework.boot.rsocket.context; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;