說明
JFinal-event 2.x
參考
Spring 4.2.x
中
Event
的使用方式而生,為
JFinal
使用者帶來更多友善。其核心目标是深層次解耦,為您節約更多時間,去陪戀人、家人和朋友 :)
年初 @idoz 提議實作類似 Spring 4.2 裡的 PayloadApplicationEvent,其實我早就有此想法,一直沒有抽出時間來折騰。
順便在 碼雲上添加了
issues 3.0優化,有2點功能:
第一點,實作 PayloadApplicationEvent 不難。擴充個 EventType
class1 為 ApplicationEvent 類 或者 PayloadApplicationEvent類資訊
class2 為 ApplicationEvent 泛型,或者 Payload 中的類資訊
第二點,将類掃描改為 annotation Processor 編譯期處理(避免容器環境差異)
由于 @EventListener 注解可以在任何方法中,annotation Processor 需要掃描
*
,然後 找出 對應的含有
@EventListener
方法的類,然後寫入
META-INF/dream.events
檔案中。
本次版本更新完成了第一點,第二點由于覺得更加适合底層架構去實作,進而友善擴充,對于 annotation Processor 感興趣的朋友可以了解我的開源
mica-auto更新說明
2019-04-08 v2.3.0
- 更新到 jfinal 3.8。
- 支援
方法可以無參數。@EventListener
-
和 jfinal Aop inject 沖突,去掉對 JFinal inject 支援,标記為棄用。ObjenesisBeanFactory
- 新增
作為 event 事件源(同 Spring PayloadApplicationEvent),event模型不再需要實作SourceClass
。ApplicationEvent
-
注解新增 value 變量,功能同 events。@EventListener
- 修複
events 參數類型判斷bug。@EventListener
初始化插件
// 初始化插件
EventPlugin plugin = new EventPlugin();
// 設定為異步,預設同步,或者使用`threadPool(ExecutorService executorService)`自定義線程池。
plugin.async();
// 設定掃描jar包,預設不掃描
plugin.scanJar();
// 設定監聽器預設包,多個包名使用;分割,預設全掃描
plugin.scanPackage("net.dreamlu");
// bean工廠,預設為DefaultBeanFactory,可實作IBeanFactory自定義擴充
// 對于将@EventListener寫在不含無參構造器的類需要使用`ObjenesisBeanFactory`(2.3.0 已經不推薦使用)
plugin.beanFactory(new ObjenesisBeanFactory());
// 手動啟動插件,用于main方法啟動,jfinal中不需要,添加插件即可。
plugin.start();
// 停止插件,用于main方法測試
plugin.stop();
建立事件類(任意模型即可 2.3.0支援)
public class AccountEvent {
private Integer id;
private String name;
private Integer age;
// 省略 get set
}
編寫監聽
@EventListener
public void listenTest1Event(AccountEvent event) {
System.out.println("AccountEvent:" + event);
}
發送事件
AccountEvent event = new AccountEvent();
event.setId(1);
event.setName("張三");
event.setAge(18);
EventKit.post(event);
@EventListener注解說明
示例
@EventListener(events = Test1Event.class, order = 1, async = true, condition = "event.isExec()")
-
或value
支援的事件類型數組,用于将事件方法定義為events
或者自定義父類。ApplicationEvent
public class Test {
// Test1Event, Test2Event 為 TestEvent 子類
@EventListener({Test1Event.class, Test2Event.class})
public void applicationEvent(TestEvent event) {
System.out.println(Thread.currentThread().getName() + "\tevent:" + event);
}
}
-
排序,數值越小越先執行,預設為order
Integer.MAX_VALUE
-
異步執行,需要插件開啟async
或者自定義線程池。async()
-
表達式條件,使用condition
判定event的屬性或者方法。event.xxxx,event.isExec() == true
jar依賴
jar包下載下傳
http://central.maven.org/maven2/net/dreamlu/JFinal-event/以上版本均已上傳到maven倉庫~
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>JFinal-event</artifactId>
<version>2.3.0</version>
</dependency>
開源推薦
-
Spring boot 微服務核心元件集: https://gitee.com/596392912/micamica
-
一款基于vue可配置化的神奇架構: https://gitee.com/smallweigit/avueAvue
-
宇宙最強微服務(架構師必備): https://gitee.com/log4j/pigpig
-
完整的線上解決方案(企業開發必備): https://gitee.com/smallc/SpringBladeSpringBlade
-
支付SDK讓支付觸手可及: https://gitee.com/javen205/IJPayIJPay