Spring Boot中初始化资源的几种方式

参考:https://blog.csdn.net/qq_28804275/article/details/80891941

Spring Boot中初始化资源的几种方式

2018年05月02日 18:06:00 阅读数:372 标签: Spring BootJava 更多

个人分类: Java

版权声明: https://blog.csdn.net/qq_28804275/article/details/80891941

  假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看。今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回答这个问题。

CommandLineRunner

  • 定义初始化类 MyCommandLineRunner
  • 实现 CommandLineRunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  • 注册成Bean,添加 @Component注解即可
  • 示例代码如下:
 
  1. @Component

  2. public class MyCommandLineRunner implements CommandLineRunner {

  3.  
  4. @Override

  5. public void run(String... args) throws Exception {

  6. System.out.println("...init resources by implements CommandLineRunner");

  7. }

  8.  
  9. }

    扫描二维码关注公众号,回复: 3125209 查看本文章

  实现了 CommandLineRunner 接口的 Component 会在所有 Spring Beans 初始化完成之后, 在 SpringApplication.run() 执行之前完成。下面通过加两行打印来验证我们的测试。

 
  1. @SpringBootApplication

  2. public class DemoApplication {

  3.  
  4. public static void main(String[] args) {

  5. System.out.println("... start SpringApplication.run()");

  6. SpringApplication.run(DemoApplication.class, args);

  7. System.out.println("... end SpringApplication.run()");

  8. }

  9.  
  10. }

  控制台打印结果如下。

 
  1. ... start SpringApplication.run()

  2.  
  3. . ____ _ __ _ _

  4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

  5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

  6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )

  7. ' |____| .__|_| |_|_| |_\__, | / / / /

  8. =========|_|==============|___/=/_/_/_/

  9. :: Spring Boot :: (v1.5.11.RELEASE)

  10. 。。。。。。(此处省略一堆打印信息)

  11. 2018-05-02 17:01:19.700 INFO 21236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

  12. ...init resources by implements CommandLineRunner

  13. 2018-05-02 17:01:19.708 INFO 21236 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.282 seconds (JVM running for 3.125)

  14. ... end SpringApplication.run()

  15.  
  16.  

ApplicationRunner

  • 定义初始化类 MyApplicationRunner
  • 实现 ApplicationRunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  • 注册成Bean,添加 @Component注解即可
  • 示例代码如下:
 
  1. @Component

  2. public class MyApplicationRunner implements ApplicationRunner {

  3.  
  4. @Override

  5. public void run(ApplicationArguments applicationArguments) throws Exception {

  6. System.out.println("...init resources by implements ApplicationRunner");

  7. }

  8.  
  9. }

  可以看到,通过实现 ApplicationRunner 接口,和通过实现 CommandLineRunner 接口都可以完成项目的初始化操作,实现相同的效果。两者之间唯一的区别是 run() 方法中自带的形参不相同,在 CommandLineRunner 中只是简单的String... args形参,而 ApplicationRunner 则是包含了 ApplicationArguments 对象,可以帮助获得更丰富的项目信息。

ApplicationArguments

@Order

  如果项目中既有实现了 ApplicationRunner 接口的初始化类,又有实现了 CommandLineRunner 接口的初始化类,那么会是哪一个先执行呢?测试告诉我们,答案是实现了 ApplicationRunner 接口的初始化类先执行,我想这点倒是不需要大家过分去关注为什么。但如果需要改变两个初始化类之间的默认执行顺序,那么使用 @Order 注解就可以帮助我们解决这个问题。

@Order

 
  1. @Component

  2. @Order(1)

  3. public class MyCommandLineRunner implements CommandLineRunner {

  4.  
  5. @Override

  6. public void run(String... args) throws Exception {

  7. System.out.println("...init resources by implements CommandLineRunner");

  8. }

  9. }

 
  1. @Component

  2. @Order(2)

  3. public class MyApplicationRunner implements ApplicationRunner {

  4.  
  5. @Override

  6. public void run(ApplicationArguments applicationArguments) throws Exception {

  7. System.out.println("...init resources by implements ApplicationRunner");

  8. }

  9.  
  10. }

  最终,控制台中打印如下。通过控制台输出我们发现, @Order 注解值越小,该初始化类也就越早执行。

 
  1. 。。。。。。(此处省略一堆打印信息)

  2. 2018-05-02 17:27:31.450 INFO 28304 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

  3. ...init resources by implements CommandLineRunner

  4. ...init resources by implements ApplicationRunner

  5. 2018-05-02 17:27:31.453 INFO 28304 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.086 seconds (JVM running for 2.977)

  6.  

@PostConstruct

  使用 @PostConstruct 注解同样可以帮助我们完成资源的初始化操作,前提是这些初始化操作不需要依赖于其它Spring beans的初始化工作。

@PostConstruct

  可以看到 @PostConstruct 注解是用在方法上的,写一个方法测试一下吧。

 
  1. @PostConstruct

  2. public void postConstruct() {

  3. System.out.println("... PostConstruct");

  4. }

  启动项目,控制台中最终打印如下。

 
  1. ... start SpringApplication.run()

  2.  
  3. . ____ _ __ _ _

  4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

  5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

  6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )

  7. ' |____| .__|_| |_|_| |_\__, | / / / /

  8. =========|_|==============|___/=/_/_/_/

  9. :: Spring Boot :: (v1.5.11.RELEASE)

  10.  
  11. 。。。。。。(此处省略一堆打印信息)

  12. ... PostConstruct

  13. 。。。。。。(此处省略一堆打印信息)

  14. 2018-05-02 17:40:22.300 INFO 29796 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

  15. ...init resources by implements CommandLineRunner

  16. ...init resources by implements ApplicationRunner

  17. 2018-05-02 17:40:22.303 INFO 29796 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.387 seconds (JVM running for 3.267)

  18. ... end SpringApplication.run()

  19.  

文末小结

  综上,使用 @PostConstruct 注解进行初始化操作的顺序是最快的,前提是这些操作不能依赖于其它Bean的初始化完成。通过添加 @Order 注解,我们可以改变同层级之间不同Bean的加载顺序。

猜你喜欢

转载自blog.csdn.net/qq_38572383/article/details/81984847