为什么要自定义ApplicationContext
在学习spring容器初始化过程中,发现spring容器预留了一些扩展点,我们可以写子类来做功能扩展,例如AbstractApplicationContext类的initPropertyResources,postProcessBeanFactory,onRefresh等方法都是空方法,留给子类来扩展用;
在spring传统框架下的扩展方式
传统的spring框架使用哪个ApplicationContext实现类,是自己写代码来指定的,下面是基于spring框架的应用启动代码:
public class DemoApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
("classpath:applicationContext.xml");
Simple bean = context.getBean(Simple.class);
bean.execute();
context.close();
}
}
如果我们要用自定的ApplicationContext实现类,只要将上面的ClassPathXmlApplicationContext改成我们做的类即可;
探索 springBoot框架下的扩展方式
先看一段代码,以下是一个springBoot应用启动的代码:
@SpringBootApplication
public class SpringApplicationStart {
public static void main(String[] args) {
SpringApplication.run(SpringApplicationStart.class, args);
}
}
上述代码可知,我们需要去查看SpringApplication.run的源码,那里应该有ApplicatinContext初始化相关信息,在SpringApplication的run方法中,果然找到了有用的信息,如下图所示,是createApplicationContext()创建了ApplicationContext的实现:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
//创建ApplicationContext的实现
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
去看看createApplicationContext方法,如下图:
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch(this.webApplicationType) {
case SERVLET:
contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
break;
case REACTIVE:
contextClass = Class.forName("org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext");
break;
default:
contextClass = Class.forName("org.springframework.context.annotation.AnnotationConfigApplicationContext");
}
} catch (ClassNotFoundException var3) {
throw new IllegalStateException("Unable create a default ApplicationContext, please specify an ApplicationContextClass", var3);
}
}
return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);
}
上述代码中有两点需要注意:
- Class<?> contextClass = this.applicationContextClass; ApplicationContext实现类来自成员变量applicationContextClass,只要我们能设置成员变量applicationContextClass,就达到目的了;
- 如果没有设置成员变量applicationContextClass就把默认的AnnotationConfigServletWebServerApplicationContext做为ApplicationContext实现类,用在spring环境中,所有我们在自定义ApplicationContext实现类时,用AnnotationConfigServletWebServerApplicationContext作为父类最合适;
如何设置自定义的ApplicationContext实现类
从createApplicationContext方法可以看出,设置了成员变量applicationContextClass,就达到了使用自定义ApplicationContext实现类的目的,那么如何设置applicationContextClass呢,就在下面这个setApplicationContextClass方法:
public void setApplicationContextClass(Class<? extends ConfigurableApplicationContext> applicationContextClass) {
this.applicationContextClass = applicationContextClass;
this.webApplicationType = WebApplicationType.deduceFromApplicationContext(applicationContextClass);
}
我们只有在启动应用的时候,在调用SpringApplication的run方法之前,先调用setApplicationContextClass方法就能指定ApplicationContext的实现类;
分析到此,你是否和我有一样的疑虑:setApplicationContextClass会不会被在其他地方调用,导致我们设置无效呢?通过以下方法来检查:
在setApplicationContextClass方法中打断点,debug启动应用,确认不会走进来;
实战使用自定义的ApplicationContext实现类
理论分析完毕,可以实战验证了:
- 基于maven创建一个springBoot的web应用,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lucas</groupId>
<artifactId>customizationApplicationVerifyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建类CustomizeApplicationContext,继承来自AnnotationConfigServletWebServerApplicationContext,重写了父类的几个方法,如下代码:
public class CustomizeApplicationContext extends AnnotationConfigServletWebServerApplicationContext {
Log logger = LogFactory.getLog(CustomizeApplicationContext.class);
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.postProcessBeanFactory(beanFactory);
logger.info("execute override postProcessBeanFactory");
}
@Override
protected void onRefresh() {
super.onRefresh();
logger.info("execute override onRefresh");
}
@Override
protected void initPropertySources() {
super.initPropertySources();
logger.info("execute override initPropertySources");
}
}
- 启动类SpringApplicationStart的main方法中,调用setApplicationContextClass方法,将ApplicationContext实现类设置为CustomizeApplicationContext
@SpringBootApplication
public class SpringApplicationStart {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(CustomizeApplicationContext.class);
springApplication.setApplicationContextClass(CustomizeApplicationContext.class);
springApplication.run(args);
}
}
- 启动应用,查看日志如下,CustomizeApplicationContext中重写的方法都被执行了,并且initPropertySources被执行了两次,那是因为除了AbstractApplicationContext类中有调用,在ServletWebServerApplicationContext类的onRefresh中会执行createWebServer方法,而createWebServer方法中也会调用一次initPropertySources方法:
2019-01-19 00:09:54.933 INFO 13380 --- [ main] c.l.springboot.SpringApplicationStart : Starting SpringApplicationStart on LAPTOP-45RFMD7C with PID 13380 (D:\ideaWork2019\blog-demo\blog_demos\customizationApplicationVerifyProject\target\classes started by 李龙龙 in D:\ideaWork2019\blog-demo\blog_demos\customizationApplicationVerifyProject)
2019-01-19 00:09:54.935 INFO 13380 --- [ main] c.l.springboot.SpringApplicationStart : No active profile set, falling back to default profiles: default
2019-01-19 00:09:55.478 INFO 13380 --- [ main] c.l.s.CustomizeApplicationContext : execute override initPropertySources
2019-01-19 00:09:55.487 INFO 13380 --- [ main] c.l.s.CustomizeApplicationContext : execute override postProcessBeanFactory
2019-01-19 00:09:58.032 INFO 13380 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-01-19 00:09:58.095 INFO 13380 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-01-19 00:09:58.095 INFO 13380 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-19 00:09:58.128 INFO 13380 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\config\jdk1.8.0_65\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\config\jdk1.8.0_65\bin;D:\config\gradle-5.1.1\bin;D:\config\Git\bin;C:\Users\李龙龙\AppData\Local\Microsoft\WindowsApps;;.]
2019-01-19 00:09:58.312 INFO 13380 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-01-19 00:09:58.312 INFO 13380 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2835 ms
2019-01-19 00:09:58.567 INFO 13380 --- [ main] c.l.s.CustomizeApplicationContext : execute override initPropertySources
2019-01-19 00:09:58.567 INFO 13380 --- [ main] c.l.s.CustomizeApplicationContext : execute override onRefresh
2019-01-19 00:09:58.831 INFO 13380 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-19 00:09:59.033 INFO 13380 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-01-19 00:09:59.036 INFO 13380 --- [ main] c.l.springboot.SpringApplicationStart : Started SpringApplicationStart in 4.423 seconds (JVM running for 5.136)
实战源码下载
本章实战的源码可以在github下载,地址和链接信息如下所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/Lucas-lilong/customizationApplicationVerifyProject | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/Lucas-lilong/customizationApplicationVerifyProject.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:Lucas-lilong/customizationApplicationVerifyProject.git | 该项目源码的仓库地址,ssh协议 |
至此,我们通过查看SpringApplication的源码,搞清楚了spring环境的ApplicationContext实现类在SpringBoot框架下如何指定,也做了一次简单的自定义实战,今后在研究和学习spring过程中,如果需要扩展spring容器就能在SpringBoot环境下进行了,相比传统的war包部署、以及指定多个jar包的classpath等操作,springboot应用的单个jar包更方便省事