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

Provide one util to distinguish bootstrap context and application context when compatible running with Spring Cloud #232

Closed
guanchao-yang opened this issue Oct 8, 2018 · 6 comments

Comments

@guanchao-yang
Copy link
Member

commented Oct 8, 2018

Question

When spring-cloud-context dependency added in classpath, the org.springframework.context.ApplicationContextInitializer implementation 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. We should add some identification to distinguish bootstrap context and application context.

Reference Issue Here

Scene and advice

Provide one util to distinguish bootstrap context and application context when compatible running with Spring Cloud. Through this util we can distinguish bootstrap context loaded by spring cloud and application context loaded by application and expected!

@QilongZhang

This comment has been minimized.

Copy link
Contributor

commented Oct 8, 2018

Spring Boot support standard way to distinguish context type. Using org.springframework.context.ApplicationContext#getId

  /**
  * Return the unique id of this application context.
  * @return the unique id of the context, or {@code null} if none
  */
  String getId();

And, i support to add an eventListener to remove all SOFABoot initializer, maybe it's a more acceptable way.

@guanchao-yang

This comment has been minimized.

Copy link
Member Author

commented Oct 8, 2018

@QilongZhang The issue suggest id but id can be changed by configuration and handled by ContextIdApplicationContextInitializer but if ApplicationContextInitializer initialized before ContextIdApplicationContextInitializer ,this way will not tack work.

i support to add an eventListener to remove all SOFABoot initializer, maybe it's a more acceptable way.

All SOFABoot initializer and other initializer only implementation ApplicationContextInitializer and we can't distinguish.And Spring Boot initialize the all ApplicationContextInitializers by org.springframework.boot.SpringApplication#getSpringFactoriesInstances(java.lang.Class<T>, java.lang.Class<?>[], java.lang.Object...) twice ,there is no event we can listen to change the behavior.

We can discuss other way also.

@QilongZhang

This comment has been minimized.

Copy link
Contributor

commented Oct 8, 2018

@guanchao-yang we can listen ApplicationEnvironmentPreparedEvent to reset the initializers. As we only care SOFABoot initializer, we can distinguish them in a special manner, such as package info.

@guanchao-yang

This comment has been minimized.

Copy link
Member Author

commented Oct 8, 2018

@QilongZhang This way can work and we should provide the specification which restrain the initializers include or exclude , such as start with com.alipay.sofa.initializers.include include and start with com.alipay.sofa.initializers.exclude exclude or through config file config include and exclude. But all the implementation key point is we should provide one util to distinguish bootstrap context and application context and based on this we can encapsulate.

v2.5.0 automation moved this from To do to Done Oct 9, 2018

@QilongZhang QilongZhang reopened this Nov 1, 2018

@QilongZhang

This comment has been minimized.

Copy link
Contributor

commented Nov 1, 2018

SOFABootEnvUtils is used to distinguish spring cloud environment. It originates from a question that initialization of SofaBootRpcInitializer in spring cloud triggers initialization of log configuration of sofa-common-tools. So we offer SOFABootEnvUtils to other starter to check whether execute in spring cloud. Actually , other than Initializer, applicationListener or post processor defined in any starter have chance to be invoked spring cloud. We could not remind them of distinguishing spring cloud or spring boot. Another solution should be offered.
sofastack/sofa-tracer#107

@QilongZhang

This comment has been minimized.

Copy link
Contributor

commented Nov 15, 2018

fixed in #268

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