Spring ApplicationListener * 用法详解
作者:闻窗 时间:2022-08-21 00:00:08
这篇文章主要介绍了Spring ApplicationListener * 用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
* 在使用过程中可以监听到某一事件的发生,进而对事件做出相应的处理。
首先自定义一个 * myListener实现ApplicationListener接口
@Repository
public class myListener implements ApplicationListener<ApplicationEvent>{
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("监听到的事件发布。。。。。。。。。。"+event.getClass());
System.out.println("监听的内容。。。。。。。。。。"+event.toString());
}
}
创建配置类MainListenerConfig:将myListener组件加入到容器中
@Configuration
@Import(myListener.class)
public class MainListenerConfig {
}
测试
public class ListenerTest {
@Test
public void test(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainListenerConfig.class);
applicationContext.publishEvent(new ApplicationEvent("我发布的事件") {});
applicationContext.close();
}
}
打印输出:可以监听到自己发布的事件和spring容器在创建实例化销毁的过程中的发布事件。
来源:https://www.cnblogs.com/tdyang/p/11890470.html
标签:spring,application,listener, ,
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis分页插件PageHelper的使用详解
2022-02-26 16:43:05
spring boot如何使用POI读取Excel文件
2022-09-19 21:26:33
![](https://img.aspxhome.com/file/2023/1/68201_0s.png)
详解java中String、StringBuilder、StringBuffer的区别
2023-06-17 06:03:23
java Long类型转为String类型的两种方式及区别说明
2022-11-12 18:03:38
Mybatis如何解决sql中like通配符模糊匹配问题
2023-12-22 19:39:52
Mybatis Plus select 实现只查询部分字段
2022-07-23 18:51:32
![](https://img.aspxhome.com/file/2023/9/61279_0s.jpg)
SpringBoot项目的配置文件中设置server.port不生效问题
2022-11-13 06:01:26
![](https://img.aspxhome.com/file/2023/6/63666_0s.png)
JVM 心得分享(加载 链接 初始化)
2023-09-01 19:17:50
C#同步和异步调用方法实例
2022-09-11 21:20:50
Java实现贪吃蛇大作战小游戏的示例代码
2023-09-25 21:16:15
![](https://img.aspxhome.com/file/2023/6/60786_0s.jpg)
Java面试基础之TCP连接以及其优化
2023-10-02 17:43:51
![](https://img.aspxhome.com/file/2023/7/66967_0s.jpg)
推荐史上最全的IDEA好用插件
2023-02-22 21:35:04
![](https://img.aspxhome.com/file/2023/8/62378_0s.png)
关于JWT之token令牌认证登录
2022-03-16 07:32:58
![](https://img.aspxhome.com/file/2023/8/69918_0s.png)
asp.net之生成验证码的方法集锦(一)
2022-09-07 22:37:13
Java新手环境搭建 Tomcat安装配置教程
2021-08-19 08:06:23
![](https://img.aspxhome.com/file/2023/4/61294_0s.jpg)
Java ArrayList深入源码层分析
2021-10-18 04:12:31
![](https://img.aspxhome.com/file/2023/9/60919_0s.png)
Mybatis下的SQL注入漏洞原理及防护方法解析
2022-06-30 18:38:29
![](https://img.aspxhome.com/file/2023/2/63082_0s.png)
全面详解Maven打包及其相关插件和高级特性
2022-03-03 21:10:00
java自定义切面增强方式(关于自定义注解aop)
2022-05-30 15:16:58
MySQL+SSM+Ajax上传图片问题
2023-01-26 04:22:52
![](https://img.aspxhome.com/file/2023/4/62974_0s.png)