diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/config/ContextRefreshedWithApplicationEvent.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/config/ContextRefreshedWithApplicationEvent.java new file mode 100644 index 00000000..1b3ac42b --- /dev/null +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/config/ContextRefreshedWithApplicationEvent.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.context.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.event.SpringApplicationEvent; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * A custom event for spring cloud context use cases that need the saved + * Spring Application. This prevents a duplicated ApplicationPreparedEvent + * from being republished. + */ +public class ContextRefreshedWithApplicationEvent extends SpringApplicationEvent { + + private final ConfigurableApplicationContext context; + + /** + * Create a new {@link ContextRefreshedWithApplicationEvent} instance. + * @param application the current application + * @param args the arguments the application is running with + * @param context the ApplicationContext about to be refreshed + */ + public ContextRefreshedWithApplicationEvent(SpringApplication application, String[] args, + ConfigurableApplicationContext context) { + super(application, args); + this.context = context; + } + + /** + * Return the application context. + * @return the context + */ + public ConfigurableApplicationContext getApplicationContext() { + return this.context; + } + +} diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java index a8644c83..08fc45ce 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java @@ -27,11 +27,11 @@ import org.springframework.boot.BootstrapRegistry; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.SpringApplication; -import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.util.Instantiator; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.context.config.ContextRefreshedWithApplicationEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; @@ -45,7 +45,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader; * @author Venil Noronha */ public class ConfigDataContextRefresher extends ContextRefresher - implements ApplicationListener { + implements ApplicationListener { private SpringApplication application; @@ -60,7 +60,7 @@ public class ConfigDataContextRefresher extends ContextRefresher } @Override - public void onApplicationEvent(ApplicationPreparedEvent event) { + public void onApplicationEvent(ContextRefreshedWithApplicationEvent event) { application = event.getSpringApplication(); } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java index a0463401..bbd841b6 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java @@ -30,8 +30,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; -import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.ContextRefreshedWithApplicationEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationEvent; @@ -51,7 +51,7 @@ import org.springframework.util.ClassUtils; * */ @Endpoint(id = "restart", enableByDefault = false) -public class RestartEndpoint implements ApplicationListener { +public class RestartEndpoint implements ApplicationListener { private static Log logger = LogFactory.getLog(RestartEndpoint.class); @@ -61,7 +61,7 @@ public class RestartEndpoint implements ApplicationListener