一、ApplicationListener的原理
ApplicationListener(應用程序監聽器)是Spring Framework中的一個事件機制,Spring在運行時觸發各類事件,如web應用程序上下文的啟動完成事件、servlet請求及響應事件、消息事件等。這些事件可以被應用程序監聽器感知到並處理,可以進行自定義的擴展和操作。
二、ApplicationListener的增加和刪除
增加ApplicationListener較為簡單,只需要實現ApplicationListener接口,並在Spring配置文件中配置即可。
public class MyEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
//do something
}
}
在Spring配置文件中添加如下配置:
刪除ApplicationListener同樣簡單,只需要將實現ApplicationListener接口的類從Spring配置文件中刪除即可。
三、ApplicationListener的作用
ApplicationListener可以處理各類應用程序事件,如ApplicationContext、ServletRequest、HttpSession等
1、ApplicationContext事件:監聽應用程序上下文的啟動和關閉事件,並可以對應用程序上下文進行自定義的擴展。
public class MyContextListener implements ApplicationListener {
@Override
public void onApplicationEvent(ContextStartedEvent event) {
ApplicationContext context = event.getApplicationContext();
//do something with context
}
}
2、ServletRequest事件:監聽servlet請求事件,可以對HttpServletRequest和HttpServletResponse進行自定義的擴展。
public class MyRequestListener implements ApplicationListener {
@Override
public void onApplicationEvent(ServletRequestEvent event) {
HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
HttpServletResponse response = (HttpServletResponse) event.getServletResponse();
//do something with request and/or response
}
}
3、HttpSession事件:監聽HttpSession的創建、銷毀、屬性變更等事件。
public class MySessionListener implements ApplicationListener {
@Override
public void onApplicationEvent(HttpSessionEvent event) {
HttpSession session = event.getSession();
//do something with session
}
}
四、ApplicationListener的用法
ApplicationListener可用於編寫各類系統插件和監聽器,可以獨立於應用程序自身。
舉一個應用場景的例子,一個電商平台在用戶下單完成後,需要將訂單信息發送給短信平台,以便發送短信給用戶,通知訂單提交成功。
第一步:定義事件
public class OrderEvent extends ApplicationEvent {
private String orderNo;
private String mobile;
public OrderEvent(Object source, String orderNo, String mobile) {
super(source);
this.orderNo = orderNo;
this.mobile = mobile;
}
public String getOrderNo() {
return orderNo;
}
public String getMobile() {
return mobile;
}
}
第二步:編寫監聽器
public class SmsListener implements ApplicationListener {
@Override
public void onApplicationEvent(OrderEvent event) {
String orderNo = event.getOrderNo();
String mobile = event.getMobile();
//send sms to mobile with orderNo
}
}
第三步:發布事件
public class OrderService {
@Autowired
private ApplicationContext ctx;
public void submitOrder(String orderNo, String mobile) {
//do something
OrderEvent event = new OrderEvent(this, orderNo, mobile);
ctx.publishEvent(event);
}
}
在訂單提交成功後調用OrderService的submitOrder方法,然後就能發送短信給用戶了。
五、ApplicationListener不執行
在某些情況下,需要限制應用程序事件的執行。有兩種方法可以做到這一點:
1、在事件監聽器中進行判斷,當不滿足條件時不執行事件
public class MyEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(SomeEvent event) {
if(/* condition not met */){
return;
}
//do something
}
}
2、使用條件註解,只有滿足條件時才會啟用事件監聽器
@Conditional(MyCondition.class)
public class MyEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(SomeEvent event) {
//do something
}
}
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//return true or false based on some condition
}
}
六、ApplicationListener加載兩次
在Spring應用程序中,有些情況下,同一個ApplicationListener會被加載多次,導致事件執行多次。針對這種情況,有以下兩種解決方案:
1、將ApplicationListener註冊為單例模式
2、在Spring配置文件中啟用事件多播器,將ApplicationListener註冊到多播器中
七、ApplicationListener指定執行順序
在一個Spring應用程序中,有可能存在多個ApplicationListener對同一事件同時進行監聽。當需要保證它們的執行順序時,可以使用@Order註解來指定它們的執行順序。
@Order(1)
public class MyFirstEventListener implements ApplicationListener {
//do something
}
@Order(2)
public class MySecondEventListener implements ApplicationListener {
//do something
}
@Order(3)
public class MyThirdEventListener implements ApplicationListener {
//do something
}
八、ApplicationListener阻止程序啟動
在某些情況下,需要在Spring應用程序啟動過程中進行一些自定義的驗證和檢查,如果不通過則需要阻止程序啟動。可以實現ApplicationListener接口的onApplicationEvent方法,並在檢查失敗時拋出異常,即可阻止程序的啟動。
public class MyContextListener implements ApplicationListener {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//do some checks
if(/*check failed*/){
throw new RuntimeException("Failed to start application");
}
}
}
九、ApplicationListenerDetector
ApplicationListenerDetector用於動態掃描項目中已經實現ApplicationListener接口的類,並將這些類自動註冊到Spring的ApplicationContext中成為監聽器。
以上是ApplicationListener的詳解,可以看到它的使用非常廣泛。通過ApplicationListener可以自定義應用程序事件及其相應的處理邏輯,可以使用AOP方式,實現應用程序的各項業務邏輯。使用Spring面向切面編程(AOP),可以為應用程序添加上更多的可重用性、擴展性、鬆散耦合性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/151667.html
微信掃一掃
支付寶掃一掃