116 Star 343 Fork 135

如梦技术 / JFinal-event

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

JFinal event 事件驱动

Mica Maven release Codacy Badge

模仿的Spring中的消息事件:详解Spring事件驱动模型

专为JFinal设计,无任何第三方依赖,小巧玲珑。

目录说明

  1. JFinal-event,JFinal-event 源码。
  2. JFinal-event-demo,JFinal-event 使用 demo。
  3. JFinal-eventx,Idea 插件 试用下载

idea 插件效果演示

如梦技术

初始化插件

// 初始化插件
EventPlugin plugin = new EventPlugin();
// 设置为异步,默认同步,或者使用`threadPool(ExecutorService executorService)`自定义线程池。
plugin.async();

// 开启类扫描,默认为 false,用于不支持注解处理器的情况,用于不使用 maven 或者不支持注解处理器的情况。
// 你也可以根据 JFinal.me().getConstants().getDevMode(),判断是否开启类扫描,因为注解处理器 maven 是支持的。
plugin.enableClassScan();
// 扫描 jar 里的 监听器,默认不扫描,在开启 enableClassScan 有效果
plugin.scanJar();
// 扫描的包,默认全扫描,可提升启动速度,在开启 enableClassScan 有效果
plugin.scanPackage("com.xxx.包名");

// 手动启动插件,用于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);
}

@EventListener
public void allEvent(Object event) {
    // 监听所有的事件
    System.out.println("allEvent:\t" + 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()")

说明

valueevents支持的事件类型数组,用于将事件方法定义为ApplicationEvent或者自定义父类。

public class Test {

    @EventListener({Test1Event.class, Test2Event.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的属性或者方法。

使用的场景优势

请参考详解Spring事件驱动模型

jar包下载 https://repo1.maven.org/maven2/net/dreamlu/JFinal-event/

以上版本均已上传到maven仓库~

maven

<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>JFinal-event</artifactId>
    <version>3.1.3</version>
</dependency>

gradle >= 5.x

api("net.dreamlu:JFinal-event:3.1.3")
annotationProcessor("net.dreamlu:JFinal-event:3.1.3")

gradle < 5.x

compile("net.dreamlu:JFinal-event:3.1.3")

注意

  • 3.0.0 由于使用了 Annotation Processor 技术,Idea 需要开启注解处理器。
  • 如果你的开发工具不支持 Annotation Processor,3.1.3 可开启类扫描。

License

( The MIT License )

开源推荐

微信公众号

如梦技术

精彩内容每日推荐!!

MIT License Copyright (c) 2019 如梦技术 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

JFinal-event事件驱动插件,无任何第三方依赖,小巧玲珑。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/596392912/JFinal-event.git
git@gitee.com:596392912/JFinal-event.git
596392912
JFinal-event
JFinal-event
master

搜索帮助