JFinal-event 2.x
参考 Spring 4.2.x
中 Event
的使用方式而生,为 JFinal
用户带来更多方便。其核心目标是深层次解耦,为您节约更多时间,去陪恋人、家人和朋友 :)
年初 @idoz 提议实现类似 Spring 4.2 里的 PayloadApplicationEvent,其实我早就有此想法,一直没有抽出时间来折腾。
顺便在 码云上添加了 issues 3.0优化 ,有2点功能:
class1 为 ApplicationEvent 类 或者 PayloadApplicationEvent类信息
class2 为 ApplicationEvent 泛型,或者 Payload 中的类信息
由于 @EventListener 注解可以在任何方法中,annotation Processor 需要扫描 *
,然后 找出 对应的含有 @EventListener
方法的类,然后写入 META-INF/dream.events
文件中。
本次版本升级完成了第一点,第二点由于觉得更加适合底层框架去实现,从而方便扩展,对于 annotation Processor 感兴趣的朋友可以了解我的开源 mica-auto
@EventListener
方法可以无参数。 ObjenesisBeanFactory
和 jfinal Aop inject 冲突,去掉对 JFinal inject 支持,标记为弃用。 SourceClass
作为 event 事件源(同 Spring PayloadApplicationEvent),event模型不再需要实现 ApplicationEvent
。 @EventListener
注解新增 value 变量,功能同 events。 @EventListener
events 参数类型判断bug。 // 初始化插件
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();
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(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.xxxx,event.isExec() == true
判定event的属性或者方法。 jar包下载
http://central.maven.org/mave...以上版本均已上传到maven仓库~
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>JFinal-event</artifactId>
<version>2.3.0</version>
</dependency>