媒介
对付一个简朴的Spring boot应用,它的spring context是只会有一个。
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的担任布局。
在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.enabled=false,然后会见对应的url,就可以看到spring context的担任环境。
如安在应用代码里获取到 Management Spring Context
假如应用代码想获取到Management Spring Context,昆山软件开发,可以通过这个bean:org.springframework.boot.actuate.autoconfigure.ManagementContextResolver
spring boot在建设Management Spring Context时,就会生存到ManagementContextResolver里。
@Configuration
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
@ConditionalOnWebApplication
@AutoConfigureAfter({ PropertyPlaceholderAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class, HypermediaAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class })
public class EndpointWebMvcAutoConfiguration
implements ApplicationContextAware, BeanFactoryAware, SmartInitializingSingleton {
@Bean
public ManagementContextResolver managementContextResolver() {
return new ManagementContextResolver(this.applicationContext);
}
@Bean
public ManagementServletContext managementServletContext(
final ManagementServerProperties properties) {
return new ManagementServletContext() {
@Override
public String getContextPath() {
return properties.getContextPath();
}
};
}
如安在Endpoints代码里获取应用的Spring context
spring boot自己没有提供要领,应用可以本身写一个@Configuration,生存应用的Spring context,然后在endpoints代码里再取出来。
ApplicationContext.setParent(ApplicationContext) 到底产生了什么