Uses a custom event to share the SpringApplication.
Rather than RestartListener republishing the ApplicationPreparedEvent, a new event, ContextRefreshedWithApplicationEvent, is published with the data from the original event. Listeners were refactored to listen to the new event instead. Fixes gh-1248
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<ApplicationPreparedEvent> {
|
||||
implements ApplicationListener<ContextRefreshedWithApplicationEvent> {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ApplicationPreparedEvent> {
|
||||
public class RestartEndpoint implements ApplicationListener<ContextRefreshedWithApplicationEvent> {
|
||||
|
||||
private static Log logger = LogFactory.getLog(RestartEndpoint.class);
|
||||
|
||||
@@ -61,7 +61,7 @@ public class RestartEndpoint implements ApplicationListener<ApplicationPreparedE
|
||||
|
||||
private String[] args;
|
||||
|
||||
private ApplicationPreparedEvent event;
|
||||
private ContextRefreshedWithApplicationEvent event;
|
||||
|
||||
private IntegrationShutdown integrationShutdown;
|
||||
|
||||
@@ -85,7 +85,7 @@ public class RestartEndpoint implements ApplicationListener<ApplicationPreparedE
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationPreparedEvent input) {
|
||||
public void onApplicationEvent(ContextRefreshedWithApplicationEvent input) {
|
||||
this.event = input;
|
||||
if (this.context == null) {
|
||||
this.context = this.event.getApplicationContext();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.context.restart;
|
||||
|
||||
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||
import org.springframework.cloud.context.config.ContextRefreshedWithApplicationEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
@@ -34,7 +35,7 @@ public class RestartListener implements SmartApplicationListener {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
private ApplicationPreparedEvent event;
|
||||
private ContextRefreshedWithApplicationEvent event;
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
@@ -56,8 +57,9 @@ public class RestartListener implements SmartApplicationListener {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent input) {
|
||||
if (input instanceof ApplicationPreparedEvent) {
|
||||
this.event = (ApplicationPreparedEvent) input;
|
||||
if (input instanceof ApplicationPreparedEvent applicationPreparedEvent) {
|
||||
this.event = new ContextRefreshedWithApplicationEvent(applicationPreparedEvent.getSpringApplication(),
|
||||
applicationPreparedEvent.getArgs(), applicationPreparedEvent.getApplicationContext());
|
||||
if (this.context == null) {
|
||||
this.context = this.event.getApplicationContext();
|
||||
}
|
||||
|
||||
@@ -30,10 +30,6 @@ public class RestartIntegrationTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfiguration.class, args);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
|
||||
Reference in New Issue
Block a user