springboot源码解析(一)-自定义系统初始化器
2021-01-03 19:30
标签:shm def led webserver 浏览器 LEDE load meta amp 本系列主要分析springboot启动过程中干了什么事情,尽量以白话的形式写出来,因为本人也很小白,望包涵。 系统初始化器是springboot在容器刷新之前执行的一个回调函数,其主要的作用就是向容器中注册属性,平时我们可能不会用到吧,但是在spring框架内部这个系统初始化器使用非常多,大家就当看看大佬是如何做的初始化,以及可以想一下他们为什么这样做,或许以后自己写程序也可以学习这种思想。 通过实现ApplicationContextInitializer接口来定义系统初始化器,这个接口是一个函数式接口,就是接口中只有一个方法,是java8的新特性。下面来看一下这个接口的定义。 Typically used within web applications that require some programmatic initialization
* of the application context. For example, registering property sources or activating
* profiles against the { { 大家可以看一下上面的解释,主要分为三段,总结下来就是: 上面对应用初始化器做了一个简单的介绍,并且看了应用初始化器的接口定义,下面就使用具体的例子来实战,自定义一些初始化,验证一下,这个先提前说一下,应用初始化有3种注入方式,具体看下面的例子。 1.自定义系统初始化器 解释一下上面代码:这个代码很简单,就是向系统环境中添加一个新的属性,属性的key是first,value是hello first,然后当这个系统初始化器被执行的时候会打印firstApplicationContextInitializer is start 2.在resource目录下新建META-INF目录,之后在META-INF目录下新建文件spring.factories,在文件中添加如下代码 解释:等号前面是系统初始化器接口的路径,这个不要改,等号后面是自定义的具体实现类,路径要写自己程序的这个类所在的路径 3.写一个controller,然后后去系统的环境,看看能不能后去到在系统初始化器中添加的first属性 ok,做完以上3部,就可以启动springboot程序了,下面是我的启动程序时的打印结果。 大家可以看到我标红的地方,就是打印的结果,下面在浏览器中调用http://localhost:8080/first 可以看到已经获取到first属性的值。 总结:第一种实现方式已经介绍完毕,大家看过之后肯定有疑问,为什么要在resource目录下搞一个META-INF,还新建一个spring.factories,为什么在spring.factories中要那样配置,憋着机,等我介绍完3中方式之后,会把源码扒拉出来,让大家看一下具体的原因,没兴趣看源码的,到时候可以不用看那部分,哈哈??,放个屁放松一下。 1.自定义系统初始化器 解释:这个和上面那个系统初始化器没什么区别,就是把first改成了second 2.修改main方法中springboot执行方式 在Springboot的启动的时候原始写法如下: 改成下面的写法就可以实现在启动的时候直接把系统初始化器注入进去 启动程序,打印结果如下 大家可以看到红色的部分,就是初始化器已经开始执行了,在浏览器中访问http://localhost:8080/second 总结:看了上面的实现,大家有没有发现一个问题在两个系统初始化器中@Order(1),优先级设置的都是1,为什么是first先打印,而second后打印呢?同样的道理,这个后面会在源码中找到原因 1.自定义系统初始化器 2.在配置文件application.perproties中配置如下配置项 同样的,这里的路径要根据自己的路径配置。 启动程序,可以看到如下打印结果 可以看到,三个系统初始化器都已经执行了,但是大家有没有发现是第三个系统初始化器先执行的,这个原因同样一会再分析。访问http://localhost:8080/third 三种实现方式已经介绍完了,如果大家只是想了解实现方式,不关心背后的源码,那么到这里就可以结束了,下一篇文章我会从源码中解释这三种方式到底是如何加载的,如果大家感兴趣可以继续向下看。 springboot源码解析(一)-自定义系统初始化器 标签:shm def led webserver 浏览器 LEDE load meta amp 原文地址:https://www.cnblogs.com/gunduzi/p/12986531.html概述
实现方式
/**
* Callback interface for initializing a Spring {@link ConfigurableApplicationContext}
* prior to being {@linkplain ConfigurableApplicationContext#refresh() refreshed}.
*
*
第一种方式:使用META-INF/spring.factories
@Order(1)
public class FirstApplicationContextInitializer implements ApplicationContextInitializer
org.springframework.context.ApplicationContextInitializer=com.example.demo.initialize.FirstApplicationContextInitializer
@RestController
public class InitializeController {
@Autowired
ApplicationContext applicationContext;
@GetMapping("/first")
public String test(){
String a = applicationContext.getEnvironment().getProperty("first");
return a;
}
@GetMapping("/second")
public String test1(){
String a = applicationContext.getEnvironment().getProperty("second");
return a;
}
@GetMapping("/third")
public String test2(){
String a = applicationContext.getEnvironment().getProperty("third");
return a;
}
}
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
firstApplicationContextInitializer is start
2020-05-29 10:20:52.989 INFO 1576 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on ganxinledeMacBook-Pro.local with PID 1576 (/Users/ganxinle/workspace/demo/target/classes started by ganxinle in /Users/ganxinle/workspace/demo)
2020-05-29 10:20:52.991 INFO 1576 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-29 10:20:53.686 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-29 10:20:53.696 INFO 1576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-29 10:20:53.696 INFO 1576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 721 ms
2020-05-29 10:20:53.860 INFO 1576 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor‘
2020-05-29 10:20:53.979 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘
2020-05-29 10:20:53.982 INFO 1576 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.258 seconds (JVM running for 1.577)
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet‘
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet‘
2020-05-29 10:21:12.354 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
第二种方式:通过直接添加的方式
@Order(1)
public class SecondApplicationContextInitializer implements ApplicationContextInitializer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(DemoApplication.class);
application.addInitializers(new SecondApplicationContextInitializer());
application.run(args);
}
}
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
firstApplicationContextInitializer is start
secondApplicationContextInitializer is start
2020-05-29 10:20:52.989 INFO 1576 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on ganxinledeMacBook-Pro.local with PID 1576 (/Users/ganxinle/workspace/demo/target/classes started by ganxinle in /Users/ganxinle/workspace/demo)
2020-05-29 10:20:52.991 INFO 1576 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-29 10:20:53.686 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-29 10:20:53.696 INFO 1576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-29 10:20:53.696 INFO 1576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 721 ms
2020-05-29 10:20:53.860 INFO 1576 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor‘
2020-05-29 10:20:53.979 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘
2020-05-29 10:20:53.982 INFO 1576 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.258 seconds (JVM running for 1.577)
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet‘
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet‘
2020-05-29 10:21:12.354 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
第三种方式:通过application.properties中配置
@Order(1)
public class ThirdApplicationContextInitializer implements ApplicationContextInitializer
context.initializer.classes=com.example.demo.initialize.ThirdApplicationContextInitializer
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
thirdApplicationContextInitializer is start
firstApplicationContextInitializer is start
secondApplicationContextInitializer is start
2020-05-29 10:20:52.989 INFO 1576 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on ganxinledeMacBook-Pro.local with PID 1576 (/Users/ganxinle/workspace/demo/target/classes started by ganxinle in /Users/ganxinle/workspace/demo)
2020-05-29 10:20:52.991 INFO 1576 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-29 10:20:53.686 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-29 10:20:53.696 INFO 1576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-29 10:20:53.696 INFO 1576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-29 10:20:53.749 INFO 1576 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 721 ms
2020-05-29 10:20:53.860 INFO 1576 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor‘
2020-05-29 10:20:53.979 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘
2020-05-29 10:20:53.982 INFO 1576 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.258 seconds (JVM running for 1.577)
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet‘
2020-05-29 10:21:12.351 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet‘
2020-05-29 10:21:12.354 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
总结
文章标题:springboot源码解析(一)-自定义系统初始化器
文章链接:http://soscw.com/index.php/essay/39885.html