前言
对于一个简单的Spring boot应用,它的spring context是只会有一个。
- 非web spring boot应用,context是AnnotationConfigApplicationContext
- web spring boot应用,context是AnnotationConfigEmbeddedWebApplicationContext
AnnotationConfigEmbeddedWebApplicationContext是spring boot里自己实现的一个context,主要功能是启动embedded servlet container,比如tomcat/jetty。
这个和传统的war包应用不一样,传统的war包应用有两个spring context。参考:http://hengyunabc.github.io/something-about-spring-mvc-webapplicationcontext/
但是对于一个复杂点的spring boot应用,它的spring context可能会是多个,下面分析下各种情况。
Demo
这个Demo展示不同情况下的spring boot context的继承情况。
https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-classloader-context
配置spring boot actuator/endpoints独立端口时
spring boot actuator默认情况下和应用共用一个tomcat,这样子的话就会直接把应用的endpoints暴露出去,带来很大的安全隐患。
尽管 Spring boot后面默认把这个关掉,需要配置management.security.enabled=false才可以访问,但是这个还是太危险了。
所以通常都建议把endpoints开在另外一个独立的端口上,比如 management.port=8081。
可以增加-Dspring.cloud.bootstrap.enabled=false,来禁止spring cloud,然后启动Demo。比如
mvn spring-boot:run -Dspring.cloud.bootstrap.enabled=false
然后打开 http://localhost:8080/ 可以看到应用的spring context继承结构。
打开 http://localhost:8081/contexttree 可以看到Management Spring Contex的继承结构。
- 可以看到当配置management独立端口时,management context的parent是应用的spring context
- 相关的实现代码在 org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration 里
在sprig cloud环境下spring context的情况
在有spring cloud时(通常是引入 spring-cloud-starter),因为spring cloud有自己的一套配置初始化机制,所以它实际上是自己启动了一个Spring context,并把自己置为应用的context的parent。
spring cloud context的启动代码在org.springframework.cloud.bootstrap.BootstrapApplicationListener里。
spring cloud context实际上是一个特殊的spring boot context,它只扫描BootstrapConfiguration。
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Use names and ensure unique to protect against duplicates List<String> names = SpringFactoriesLoader .loadFactoryNames(BootstrapConfiguration.class, classLoader); for (String name : StringUtils.commaDelimitedListToStringArray( environment.getProperty("spring.cloud.bootstrap.sources", ""))) { names.add(name); } // TODO: is it possible or sensible to share a ResourceLoader? SpringApplicationBuilder builder = new SpringApplicationBuilder() .profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF) .environment(bootstrapEnvironment) .properties("spring.application.name:" + configName) .registerShutdownHook(false).logStartupInfo(false).web(false); List<Class<?>> sources = new ArrayList<>();
最终会把这个ParentContextApplicationContextInitializer加到应用的spring context里,来把自己设置为应用的context的parent。
public class ParentContextApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered { private int order = Ordered.HIGHEST_PRECEDENCE; private final ApplicationContext parent; @Override public void initialize(ConfigurableApplicationContext applicationContext) { if (applicationContext != this.parent) { applicationContext.setParent(this.parent); applicationContext.addApplicationListener(EventPublisher.INSTANCE); } }
和上面一样,直接启动demo,不要配置-Dspring.cloud.bootstrap.enabl