JFinal-event 2.x
参考 Spring 4.2.x
中 Event
的使用方式而生,为 JFinal
用户带来更多方便。其核心目标是深层次解耦,为您节约更多时间,去陪恋人、家人和朋友 :)vue
年初 @idoz 提议实现相似 Spring 4.2 里的 PayloadApplicationEvent,其实我早就有此想法,一直没有抽出时间来折腾。java
顺便在 码云上添加了 issues 3.0优化,有2点功能:git
class1 为 ApplicationEvent 类 或者 PayloadApplicationEvent类信息 class2 为 ApplicationEvent 泛型,或者 Payload 中的类信息数组
因为 @EventListener 注解能够在任何方法中,annotation Processor 须要扫描 *
,而后 找出 对应的含有 @EventListener
方法的类,而后写入 META-INF/dream.events
文件中。架构
本次版本升级完成了第一点,第二点因为以为更加适合底层框架去实现,从而方便扩展,对于 annotation Processor 感兴趣的朋友能够了解个人开源 mica-autoapp
@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包下载 central.maven.org/maven2/net/…框架
以上版本均已上传到maven仓库~异步
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>JFinal-event</artifactId>
<version>2.3.0</version>
</dependency>
复制代码
mica
Spring boot 微服务核心组件集:gitee.com/596392912/m…Avue
一款基于vue可配置化的神奇框架:gitee.com/smallweigit…pig
宇宙最强微服务(架构师必备):gitee.com/log4j/pigSpringBlade
完整的线上解决方案(企业开发必备):gitee.com/smallc/Spri…IJPay
支付SDK让支付触手可及:gitee.com/javen205/IJ…扫描上面二维码,更多精彩内容天天推荐!async