目錄html
在平常開發時,咱們經常須要 在SpringBoot 應用啓動時執行某一段邏輯,以下面的場景:java
在實現這些功能時,咱們可能會遇到一些"坑"。 爲了利用SpringBoot框架的便利性,咱們不得不將整個應用的執行控制權交給容器,因而形成了你們對於細節是一無所知的。
那麼在實現初始化邏輯代碼時就須要當心了,好比,咱們並不能簡單的將初始化邏輯在Bean類的構造方法中實現,相似下面的代碼:spring
@Component public class InvalidInitExampleBean { @Autowired private Environment env; public InvalidInitExampleBean() { env.getActiveProfiles(); } }
這裏,咱們在InvalidInitExampleBean的構造方法中試圖訪問一個自動注入的env字段,當真正執行時,你必定會獲得一個空指針異常(NullPointerException)。
緣由在於,當構造方法被調用時,Spring上下文中的Environment這個Bean極可能尚未被實例化,同時也仍未注入到當前對象,因此並不能這樣進行調用。數據庫
下面,咱們來看看在SpringBoot中實現"安全初始化"的一些方法:安全
@PostConstruct 註解實際上是來自於 javax的擴展包中(大多數人的印象中是來自於Spring框架),它的做用在於聲明一個Bean對象初始化完成後執行的方法。
來看看它的原始定義:springboot
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
也就是說,該方法會在全部依賴字段注入後才執行,固然這一動做也是由Spring框架執行的。app
下面的代碼演示了使用@PostConstruct的例子:框架
@Component public class PostConstructExampleBean { private static final Logger LOG = Logger.getLogger(PostConstructExampleBean.class); @Autowired private Environment environment; @PostConstruct public void init() { //environment 已經注入 LOG.info(Arrays.asList(environment.getDefaultProfiles())); } }
InitializingBean 是由Spring框架提供的接口,其與@PostConstruct註解的工做原理很是相似。
若是不使用註解的話,你須要讓Bean實例繼承 InitializingBean接口,並實現afterPropertiesSet()這個方法。ide
下面的代碼,展現了這種用法:post
@Component public class InitializingBeanExampleBean implements InitializingBean { private static final Logger LOG = Logger.getLogger(InitializingBeanExampleBean.class); @Autowired private Environment environment; @Override public void afterPropertiesSet() throws Exception { //environment 已經注入 LOG.info(Arrays.asList(environment.getDefaultProfiles())); } }
咱們在聲明一個Bean的時候,能夠同時指定一個initMethod屬性,該屬性會指向Bean的一個方法,表示在初始化後執行。
以下所示:
@Bean(initMethod="init") public InitMethodExampleBean exBean() { return new InitMethodExampleBean(); }
而後,這裏將initMethod指向init方法,相應的咱們也須要在Bean中實現這個方法:
public class InitMethodExampleBean { private static final Logger LOG = Logger.getLogger(InitMethodExampleBean.class); @Autowired private Environment environment; public void init() { LOG.info(Arrays.asList(environment.getDefaultProfiles())); } }
上面的代碼是基於Java註解的方式,使用Xml配置也能夠達到一樣的效果:
<bean id="initMethodExampleBean" class="org.baeldung.startup.InitMethodExampleBean" init-method="init"> </bean>
該方式在早期的 Spring版本中大量被使用
若是依賴的字段在Bean的構造方法中聲明,那麼Spring框架會先實例這些字段對應的Bean,再調用當前的構造方法。
此時,構造方法中的一些操做也是安全的,以下:
@Component public class LogicInConstructorExampleBean { private static final Logger LOG = Logger.getLogger(LogicInConstructorExampleBean.class); private final Environment environment; @Autowired public LogicInConstructorExampleBean(Environment environment) { //environment實例已初始化 this.environment = environment; LOG.info(Arrays.asList(environment.getDefaultProfiles())); } }
ApplicationListener 是由 spring-context組件提供的一個接口,主要是用來監聽 "容器上下文的生命週期事件"。
它的定義以下:
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }
這裏的event能夠是任何一個繼承於ApplicationEvent的事件對象。 對於初始化工做來講,咱們能夠經過監聽ContextRefreshedEvent這個事件來捕捉上下文初始化的時機。
以下面的代碼:
@Component public class StartupApplicationListenerExample implements ApplicationListener<ContextRefreshedEvent> { private static final Logger LOG = Logger.getLogger(StartupApplicationListenerExample.class); public static int counter; @Override public void onApplicationEvent(ContextRefreshedEvent event) { LOG.info("Increment counter"); counter++; } }
在Spring上下文初始化完成後,這裏定義的方法將會被執行。
與前面的InitializingBean不一樣的是,經過ApplicationListener監聽的方式是全局性的,也就是當全部的Bean都初始化完成後纔會執行方法。
Spring 4.2 以後引入了新的 @EventListener註解,能夠實現一樣的效果:
@Component public class EventListenerExampleBean { private static final Logger LOG = Logger.getLogger(EventListenerExampleBean.class); public static int counter; @EventListener public void onApplicationEvent(ContextRefreshedEvent event) { LOG.info("Increment counter"); counter++; } }
SpringBoot 提供了一個CommanLineRunner接口,用來實如今應用啓動後的邏輯控制,其定義以下:
public interface CommandLineRunner { /** * Callback used to run the bean. * @param args incoming main method arguments * @throws Exception on error */ void run(String... args) throws Exception; }
這裏的run方法會在Spring 上下文初始化完成後執行,同時會傳入應用的啓動參數。
以下面的代碼:
@Component public class CommandLineAppStartupRunner implements CommandLineRunner { private static final Logger LOG = LoggerFactory.getLogger(CommandLineAppStartupRunner.class); public static int counter; @Override public void run(String...args) throws Exception { //上下文已初始化完成 LOG.info("Increment counter"); counter++; } }
此外,對於多個CommandLineRunner的狀況下可使用@Order註解來控制它們的順序。
與 CommandLineRunner接口相似, Spring boot 還提供另外一個ApplicationRunner 接口來實現初始化邏輯。
不一樣的地方在於 ApplicationRunner.run()方法接受的是封裝好的ApplicationArguments參數對象,而不是簡單的字符串參數。
@Component public class AppStartupRunner implements ApplicationRunner { private static final Logger LOG = LoggerFactory.getLogger(AppStartupRunner.class); public static int counter; @Override public void run(ApplicationArguments args) throws Exception { LOG.info("Application started with option names : {}", args.getOptionNames()); LOG.info("Increment counter"); counter++; } }
ApplicationArguments對象提供了一些很是方便的方法,能夠用來直接獲取解析後的參數,好比:
java -jar application.jar --debug --ip=xxxx
此時經過 ApplicationArguments的getOptionNames就會獲得["debug","ip"]這樣的值
下面,經過一個小測試來演示幾種初始化方法的執行次序,按以下代碼實現一個複合式的Bean:
@Component @Scope(value = "prototype") public class AllStrategiesExampleBean implements InitializingBean { private static final Logger LOG = Logger.getLogger(AllStrategiesExampleBean.class); public AllStrategiesExampleBean() { LOG.info("Constructor"); } @Override public void afterPropertiesSet() throws Exception { LOG.info("InitializingBean"); } @PostConstruct public void postConstruct() { LOG.info("PostConstruct"); } //在XML中定義爲initMethod public void init() { LOG.info("init-method"); } }
執行這個Bean的初始化,會發現日誌輸出以下:
[main] INFO o.b.startup.AllStrategiesExampleBean - Constructor [main] INFO o.b.startup.AllStrategiesExampleBean - PostConstruct [main] INFO o.b.startup.AllStrategiesExampleBean - InitializingBean [main] INFO o.b.startup.AllStrategiesExampleBean - init-method
因此,這幾種初始化的順序爲:
https://www.baeldung.com/running-setup-logic-on-startup-in-spring