spring boot实战(第二篇)事件监听
前言
spring boot在啟動(dòng)過(guò)程中增加事件監(jiān)聽(tīng)機(jī)制,為用戶功能拓展提供極大的便利。
支持的事件類型四種
ApplicationStartedEvent
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
ApplicationFailedEvent
實(shí)現(xiàn)監(jiān)聽(tīng)步驟:
1.監(jiān)聽(tīng)類實(shí)現(xiàn)ApplicationListener接口?
2.將監(jiān)聽(tīng)類添加到SpringApplication實(shí)例
ApplicationStartedEvent
ApplicationStartedEvent:spring boot啟動(dòng)開(kāi)始時(shí)執(zhí)行的事件
創(chuàng)建對(duì)應(yīng)的監(jiān)聽(tīng)類?MyApplicationStartedEventListener.java
?
package com.lkl.springboot.listener;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener;/*** spring boot 啟動(dòng)監(jiān)聽(tīng)類* * @author liaokailin* @version $Id: MyApplicationStartedEventListener.java, v 0.1 2015年9月2日 下午11:06:04 liaokailin Exp $*/ public class MyApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationStartedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {SpringApplication app = event.getSpringApplication();app.setShowBanner(false);// 不顯示banner信息logger.info("==MyApplicationStartedEventListener==");} }?
?
?
?
在該事件中可以獲取到SpringApplication對(duì)象,可做一些執(zhí)行前的設(shè)置.
?
Application.java類
?
package com.lkl.springboot;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import com.lkl.springboot.listener.MyApplicationStartedEventListener;@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication app = new SpringApplication(Application.class); app.addListeners(new MyApplicationStartedEventListener());app.run(args);} }?
?
?
ApplicationEnvironmentPreparedEvent
ApplicationEnvironmentPreparedEvent:spring boot 對(duì)應(yīng)Enviroment已經(jīng)準(zhǔn)備完畢,但此時(shí)上下文context還沒(méi)有創(chuàng)建。
MyApplicationEnvironmentPreparedEventListener.java
?
package com.lkl.springboot.listener;import java.util.Iterator;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource;/*** spring boot 配置環(huán)境事件監(jiān)聽(tīng)* @author liaokailin* @version $Id: MyApplicationEnvironmentPreparedEventListener.java, v 0.1 2015年9月2日 下午11:21:15 liaokailin Exp $*/ public class MyApplicationEnvironmentPreparedEventListener implementsApplicationListener<ApplicationEnvironmentPreparedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationEnvironmentPreparedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {ConfigurableEnvironment envi = event.getEnvironment();MutablePropertySources mps = envi.getPropertySources();if (mps != null) {Iterator<PropertySource<?>> iter = mps.iterator();while (iter.hasNext()) {PropertySource<?> ps = iter.next();logger.info("ps.getName:{};ps.getSource:{};ps.getClass:{}", ps.getName(), ps.getSource(), ps.getClass());}}}}?
?
?
?
?
在該監(jiān)聽(tīng)中獲取到ConfigurableEnvironment后可以對(duì)配置信息做操作,例如:修改默認(rèn)的配置信息,增加額外的配置信息等等~~~
ApplicationPreparedEvent
ApplicationPreparedEvent:spring boot上下文context創(chuàng)建完成,但此時(shí)spring中的bean是沒(méi)有完全加載完成的。
MyApplicationPreparedEventListener.java
?
package com.lkl.springboot.listener;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext;/*** 上下文創(chuàng)建完成后執(zhí)行的事件監(jiān)聽(tīng)器* * @author liaokailin* @version $Id: MyApplicationPreparedEventListener.java, v 0.1 2015年9月2日 下午11:29:38 liaokailin Exp $*/ public class MyApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationPreparedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationPreparedEvent event) {ConfigurableApplicationContext cac = event.getApplicationContext();passContextInfo(cac);}/*** 傳遞上下文* @param cac*/private void passContextInfo(ApplicationContext cac) {//dosomething()}}?
?
?
?
?
在獲取完上下文后,可以將上下文傳遞出去做一些額外的操作。
在該監(jiān)聽(tīng)器中是無(wú)法獲取自定義bean并進(jìn)行操作的。
ApplicationFailedEvent
ApplicationFailedEvent:spring boot啟動(dòng)異常時(shí)執(zhí)行事件?
MyApplicationFailedEventListener.java
?
package com.lkl.springboot.listener;import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.context.ApplicationListener;public class MyApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {@Overridepublic void onApplicationEvent(ApplicationFailedEvent event) {Throwable throwable = event.getException();handleThrowable(throwable);}/*處理異常*/private void handleThrowable(Throwable throwable) {}}?
?
?
?
?
在異常發(fā)生時(shí),最好是添加虛擬機(jī)對(duì)應(yīng)的鉤子進(jìn)行資源的回收與釋放,能友善的處理異常信息。
在spring boot中已經(jīng)為大家考慮了這一點(diǎn),默認(rèn)情況開(kāi)啟了對(duì)應(yīng)的功能:
?
public void registerShutdownHook() {if (this.shutdownHook == null) {// No shutdown hook registered yet.this.shutdownHook = new Thread() {@Overridepublic void run() {doClose();}};Runtime.getRuntime().addShutdownHook(this.shutdownHook);}}?
在doClose()方法中進(jìn)行資源的回收與釋放。
總結(jié)
以上是生活随笔為你收集整理的spring boot实战(第二篇)事件监听的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot(1.5.6.REL
- 下一篇: spring boot实战(第四篇)分散