JFinal-event事件驱动插件,无任何第三方依赖,小巧玲珑。
一晃Git@OSC上有5个多月没怎么提交代码了,前段时间群里的朋友也说他们公司也在用JFinal-event,深感荣幸。
国庆前偶然看到Spring 4.2框架中注释驱动的事件监听器详解,确实比以前方便了不少。于是新建了一个issues要实现成类似的功能。
注意:由于比1.x版本更加灵活,故没去做1.x版本的兼容!
1. 初始化插件
EventPlugin plugin = new EventPlugin();
// 设置为异步,默认同步,或者使用threadPool(ExecutorService executorService)自定义线程池。
plugin.async();
// 设置扫描jar包,默认不扫描
plugin.scanJar();
// 设置监听器默认包,默认全扫描
plugin.scanPackage("net.dreamlu");
// 手动启动插件,用于main方法启动,jfinal中不需要,添加插件即可。
plugin.start();
// 停止插件,用于main方法测试
plugin.stop();
2. 事件类
// 继承 ApplicationEvent
public class Test1Event extends ApplicationEvent {
private static final long serialVersionUID = 6994987952247306131L;
public Test1Event(Object source) {
super(source);
}
}
3. 监听事件
@EventListener(order = 1, events = Test1Event.class)
public void xxxx(ApplicationEvent event) {
Object xx = event.getSource();
System.out.println(Thread.currentThread().getName() + " " + this.getClass() + " " + "\tsource:" + xx);
}
@EventListener
public void xxxx(Test1Event event) {
Object xx = event.getSource();
System.out.println(Thread.currentThread().getName() + " " + this.getClass() + " " + "\tsource:" + xx);
}
4. 发送事件
EventKit.post(new Test1Event("hello1"));
@EventListener注解说明
@EventListener(events = Test1Event.class, order = 1, async = true, condition = "event.isExec()")
events支持的事件类型数组,用于将事件方法定义为ApplicationEvent或者自定义父类。
@EventListener(events = Test1Event.class)
public void applicationEvent(ApplicationEvent event) {
String xx = (String) event.getSource();
System.out.println(Thread.currentThread().getName() + "\tsource:" + xx);
}
order排序,数值越小越先执行,默认为Integer.MAX_VALUE
async异步执行,需要插件开启async()或者自定义线程池。
condition表达式条件,使用event.xxxx,event.isExec() == true判定event的属性或者方法。
Maven引入:
<dependency> <groupId>net.dreamlu</groupId> <artifactId>JFinal-event</artifactId> <version>2.0.0</version> </dependency>
Jar包下来:http://central.maven.org/maven2/net/dreamlu/JFinal-event/
引用来自“如梦技术”的评论
😃刚把代码修改了下升级到2.0.1,可以自定义Bean工厂方便IOC接入。晚上回去了推maven引用来自“JFinal”的评论
发新版本一点消息都没有 😀