Commit 0cbfff96 authored by nobodyiam's avatar nobodyiam

fix the executing sequence of initializeSystemProperty

parent fe8162db
...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; ...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.ctrip.framework.apollo.spring.util.SpringInjector; import com.ctrip.framework.apollo.spring.util.SpringInjector;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
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.boot.SpringApplication;
...@@ -17,8 +18,6 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -17,8 +18,6 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.List;
/** /**
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase * Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
* *
...@@ -49,10 +48,9 @@ import java.util.List; ...@@ -49,10 +48,9 @@ import java.util.List;
* apollo.bootstrap.eagerLoad.enabled = true * apollo.bootstrap.eagerLoad.enabled = true
* </pre> * </pre>
* *
* This would be very helpful when your logging configurations is set by Apollo. * This would be very helpful when your logging configurations is set by Apollo.
*
* for example, you have defined logback-spring.xml in your project , and you want to inject some attributes into logback-spring.xml.
* *
* for example, you have defined logback-spring.xml in your project, and you want to inject some attributes into logback-spring.xml.
* *
*/ */
public class ApolloApplicationContextInitializer implements public class ApolloApplicationContextInitializer implements
...@@ -69,8 +67,6 @@ public class ApolloApplicationContextInitializer implements ...@@ -69,8 +67,6 @@ public class ApolloApplicationContextInitializer implements
public void initialize(ConfigurableApplicationContext context) { public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment(); ConfigurableEnvironment environment = context.getEnvironment();
initializeSystemProperty(environment);
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)) {
logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED); logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
...@@ -83,11 +79,8 @@ public class ApolloApplicationContextInitializer implements ...@@ -83,11 +79,8 @@ public class ApolloApplicationContextInitializer implements
/** /**
*
*
* Initialize Apollo Configurations Just after environment is ready. * Initialize Apollo Configurations Just after environment is ready.
* *
*
* @param environment * @param environment
*/ */
protected void initialize(ConfigurableEnvironment environment) { protected void initialize(ConfigurableEnvironment environment) {
...@@ -136,16 +129,12 @@ public class ApolloApplicationContextInitializer implements ...@@ -136,16 +129,12 @@ public class ApolloApplicationContextInitializer implements
/** /**
* *
* In order to load Apollo configurations even as early before Spring loading logging system phase, * In order to load Apollo configurations as early as even before Spring loading logging system phase,
* * this EnvironmentPostProcessor can be called Just After ConfigFileApplicationListener has succeeded.
* This EnvironmentPostProcessor can be called Just After ConfigFileApplicationListener has succeeded.
*
*
* The processing sequence would be like this:
*
* Load Bootstrap properties and application properties -----> load Apollo configuration properties ----> Initialize Logging systems
*
* *
* <br />
* The processing sequence would be like this: <br />
* Load Bootstrap properties and application properties -----> load Apollo configuration properties ----> Initialize Logging systems
* *
* @param configurableEnvironment * @param configurableEnvironment
* @param springApplication * @param springApplication
...@@ -153,18 +142,20 @@ public class ApolloApplicationContextInitializer implements ...@@ -153,18 +142,20 @@ public class ApolloApplicationContextInitializer implements
@Override @Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) { public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
Boolean eagerLoadEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,Boolean.class,false); // should always initialize system properties like app.id in the first place
initializeSystemProperty(configurableEnvironment);
Boolean eagerLoadEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.class, false);
//EnvironmentPostProcessor should not be triggered if you don't want Apollo Loading before Logging System Initialization //EnvironmentPostProcessor should not be triggered if you don't want Apollo Loading before Logging System Initialization
if(!eagerLoadEnabled){ if (!eagerLoadEnabled) {
return; return;
} }
Boolean bootstrapEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED,Boolean.class,false); Boolean bootstrapEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false);
if(bootstrapEnabled) { if (bootstrapEnabled) {
initialize(configurableEnvironment); initialize(configurableEnvironment);
} }
} }
......
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