Commit 05acc044 by nobodyiam

use ApplicationContextInitializer instead of SpringApplicationRunListener for better compatibility

parent a81c9e49
...@@ -6,16 +6,12 @@ import com.ctrip.framework.apollo.build.ApolloInjector; ...@@ -6,16 +6,12 @@ import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory; import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.ctrip.framework.apollo.spring.config.ConfigPropertySource;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
...@@ -36,28 +32,16 @@ import org.springframework.core.env.ConfigurableEnvironment; ...@@ -36,28 +32,16 @@ import org.springframework.core.env.ConfigurableEnvironment;
* apollo.bootstrap.namespaces = application,FX.apollo * apollo.bootstrap.namespaces = application,FX.apollo
* </pre> * </pre>
*/ */
public class ApolloSpringApplicationRunListener implements SpringApplicationRunListener, public class ApolloApplicationContextInitializer implements
PriorityOrdered { ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger logger = LoggerFactory.getLogger(ApolloSpringApplicationRunListener.class); private static final Logger logger = LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults(); private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
private final ConfigPropertySourceFactory configPropertySourceFactory = ApolloInjector private final ConfigPropertySourceFactory configPropertySourceFactory = ApolloInjector
.getInstance(ConfigPropertySourceFactory.class); .getInstance(ConfigPropertySourceFactory.class);
public ApolloSpringApplicationRunListener(SpringApplication application, String[] args) { @Override
//ignore public void initialize(ConfigurableApplicationContext context) {
}
public void starting() {
}
public void started() {
}
public void environmentPrepared(ConfigurableEnvironment environment) {
}
public void contextPrepared(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment(); ConfigurableEnvironment environment = context.getEnvironment();
String enabled = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "false"); String enabled = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "false");
if (!Boolean.valueOf(enabled)) { if (!Boolean.valueOf(enabled)) {
...@@ -84,15 +68,4 @@ public class ApolloSpringApplicationRunListener implements SpringApplicationRunL ...@@ -84,15 +68,4 @@ public class ApolloSpringApplicationRunListener implements SpringApplicationRunL
environment.getPropertySources().addFirst(composite); environment.getPropertySources().addFirst(composite);
} }
public void contextLoaded(ConfigurableApplicationContext context) {
}
public void finished(ConfigurableApplicationContext configurableApplicationContext,
Throwable throwable) {
}
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
} }
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration
org.springframework.boot.SpringApplicationRunListener=\ org.springframework.context.ApplicationContextInitializer=\
com.ctrip.framework.apollo.spring.boot.ApolloSpringApplicationRunListener com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer
...@@ -5,6 +5,7 @@ import java.io.BufferedReader; ...@@ -5,6 +5,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -24,7 +25,12 @@ public class SpringBootSampleApplication { ...@@ -24,7 +25,12 @@ public class SpringBootSampleApplication {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ApplicationContext context = new SpringApplicationBuilder(SpringBootSampleApplication.class).run(args); ApplicationContext context = new SpringApplicationBuilder(SpringBootSampleApplication.class).run(args);
AnnotatedBean annotatedBean = context.getBean(AnnotatedBean.class); AnnotatedBean annotatedBean = context.getBean(AnnotatedBean.class);
SampleRedisConfig redisConfig = context.getBean(SampleRedisConfig.class); SampleRedisConfig redisConfig = null;
try {
redisConfig = context.getBean(SampleRedisConfig.class);
} catch (NoSuchBeanDefinitionException ex) {
System.out.println("SampleRedisConfig is null, 'redis.cache.enabled' must have been set to false.");
}
System.out.println("SpringBootSampleApplication Demo. Input any key except quit to print the values. Input quit to exit."); System.out.println("SpringBootSampleApplication Demo. Input any key except quit to print the values. Input quit to exit.");
while (true) { while (true) {
...@@ -35,7 +41,9 @@ public class SpringBootSampleApplication { ...@@ -35,7 +41,9 @@ public class SpringBootSampleApplication {
} }
System.out.println(annotatedBean.toString()); System.out.println(annotatedBean.toString());
System.out.println(redisConfig.toString()); if (redisConfig != null) {
System.out.println(redisConfig.toString());
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment