Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When spring-cloud-context dependency added in classpath, the `org.springframework.context.ApplicationContextInitializer` will be called twice. #1151

Closed
guanchao-yang opened this issue Sep 28, 2018 · 6 comments

Comments

@guanchao-yang
Copy link

commented Sep 28, 2018

Question

When spring-cloud-context dependency added in classpath, the org.springframework.context.ApplicationContextInitializer will be called twice. If we have many ApplicationContextInitializer implementation with Spring Boot ,when spring-cloud-context dependency
added ,our application starter ApplicationContextInitializer run twice. If we have and best practice or will add some identification to distinguish bootstrap context and application context?

Bug report

When spring-cloud-context dependency added in classpath, the org.springframework.context.ApplicationContextInitializer will be called twice.

Enhancement

Suggest that when init twice org.springframework.context.ApplicationContextInitializer ,we should have obvious identification to distinguish bootstrap context and application context。

Thank you so much for your reply and advice!

@guanchao-yang

This comment has been minimized.

Copy link
Author

commented Sep 28, 2018

@ryanjbaxter @spencergibb Thank you so much for your reply and advice!

@spencergibb

This comment has been minimized.

Copy link
Member

commented Sep 28, 2018

This is not a bug. It is the way the bootstrap context is loaded.

@guanchao-yang

This comment has been minimized.

Copy link
Author

commented Sep 28, 2018

@spencergibb @ramaiahkk Can we provide identification to distinguish bootstrap context and application context?

@spencergibb

This comment has been minimized.

Copy link
Member

commented Sep 28, 2018

Check the ID of the context

@guanchao-yang

This comment has been minimized.

Copy link
Author

commented Sep 28, 2018

Check the ID of the context

The Bootstrap Application Context maybe like org.springframework.context.annotation.AnnotationConfigApplicationContext@5d2a4eed before org.springframework.boot.context.ContextIdApplicationContextInitializer#initialize handled!!!

I think this way will be ok from your official document Here.

The additional property sources are:

"bootstrap": an optional CompositePropertySource appears with high priority if any 
PropertySourceLocators are found in the Bootstrap context, and they have non-empty 
properties. An example would be properties from the Spring Cloud Config Server. See
 below for instructions on how to customize the contents of this property source.

We can use environment to distinguish bootstrap context and application context like this:

public static boolean isSpringCloudBootstrapEnvironment(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
        PropertySource propertySource = configurableEnvironment.getPropertySources().get(BOOTSTRAP_PROPERTY_SOURCE_NAME);
        if (propertySource != null) {
            logger.debug("Current application context environment is bootstrap");
            return true;
        }
    }
    return false;
}

@spencergibb @ramaiahkk What do you think about?Or give some suggestion, Thank you very much

@guanchao-yang

This comment has been minimized.

Copy link
Author

commented Sep 28, 2018

@spencergibb @ramaiahkk Thank you and Waiting for your answer !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.