Spring boot 启动初始化探索

实现 InitializingBean 接口

重写 afterPropertiesSet 方法, 将在 bean 初始化完成后执行该方法.

实现 CommandLineRunner 接口

实现 run 接口, @Order注解规定了CommandLineRunner实例的运行顺序。@order(value=2) value 的值从小到大依次执行。

Spring boot 启动过程

  • @SpringBootApplication
    • @SpringBootConfiguration, 继承自Spring的 @Configuration 注解, spring 3.0中增加了@Configuration,@Bean。可基于JavaConfig形式对 Spring 容器中的bean进行更直观的配置。SpringBoot推荐使用基于JavaConfig的配置形式。
    • @EnableAutoConfiguration
    • @ComponentScan, @ComponentScan通常与@Configuration一起配合使用,相当于xml里面的context:component-scan,用来告诉Spring需要扫描哪些包或类。如果不设值的话默认扫描@ComponentScan注解所在类的同级类和同级目录下的所有类,所以对于一个Spring Boot项目,一般会把入口类放在顶层目录中,这样就能够保证源码目录下的所有类都能够被扫描到。

参考文献