SpringBoot使用自定义事件监听器的demo

记录一下SpringBoot自定义事件监听器的使用方法

案例源码:SpringBoot使用自定义事件监听器的demo

使用的SpringBoot2.0.x版本

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>

自定义事件监听: 自定义事件 和 自定义监听两部分

自定义事件
继承自ApplicationEvent抽象类,定义自己的构造器

自定义监听
实现ApplicationListener接口,然后实现onApplicationEvent方法

首先自定义事件
ApplicationEvent抽象类
myw
实现继承,用对象方式,新建对象

DemoUser.java

package boot.example.event.events;public class DemoUser {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

EventDemoUser.java

package boot.example.event.events;import org.springframework.context.ApplicationEvent;public class EventDemoUser extends ApplicationEvent {private final DemoUser demoUser;public EventDemoUser(Object source, DemoUser demoUser) {super(source);this.demoUser = demoUser;}public DemoUser getDemoUser() {return demoUser;}
}

ApplicationListener接口
myw
自定义监听类
EventDemoUserListener.java

package boot.example.event.listener;import boot.example.event.events.EventDemoUser;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;//  将监听器装载入spring容器
@Component
//@Service
public class EventDemoUserListener implements ApplicationListener<EventDemoUser> {@Async@Overridepublic void onApplicationEvent(EventDemoUser eventDemoUser) {System.out.println("自定义事件监听器(EventDemoUserListener)收到发布的消息: " + eventDemoUser.getDemoUser().getUsername());System.out.println("自定义事件监听器(EventDemoUserListener)收到发布的消息: " + eventDemoUser.getDemoUser().getPassword());}
}

我这里使用接口的方式来触发
EventDemoUserController.java

package boot.example.event.controller;import boot.example.event.events.DemoUser;
import boot.example.event.events.EventDemo;
import boot.example.event.events.EventDemoUser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping(value="/eventDemoUser")
public class EventDemoUserController {@Resourceprivate ApplicationContext applicationContext;@RequestMapping(value="/test1")public String test1() {DemoUser demoUser = new DemoUser();demoUser.setUsername("admin");demoUser.setPassword("123456");applicationContext.publishEvent(new EventDemoUser(this, demoUser));return "testEventDemo";}@Resourceprivate ApplicationEventPublisher applicationEventPublisher;@RequestMapping(value="/test2")public String test2() {DemoUser demoUser = new DemoUser();demoUser.setUsername("admin2");demoUser.setPassword("654321");applicationContext.publishEvent(new EventDemoUser(this, demoUser));return "testEventDemo";}}

这里使用了两种触发方式

@Resource
private ApplicationContext applicationContext;
@Resource
private ApplicationEventPublisher applicationEventPublisher;

控制台输出查看
myw
自定义监听的几种方式

1.启动时手动向ApplicationContext中添加监听器

EventDemo.java

package boot.example.event.events;import org.springframework.context.ApplicationEvent;public class EventDemo extends ApplicationEvent {public EventDemo(Object source) {super(source);}
}

AppEventDemo.java

package boot.example.event;import boot.example.event.listener.EventDemoListener1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication
@EnableAsync
public class AppEventDemo {public static void main( String[] args ) {ConfigurableApplicationContext context = SpringApplication.run(AppEventDemo.class, args);// 手动向ApplicationContext中添加监听器context.addApplicationListener(new EventDemoListener1());System.out.println( "Hello World!" );}//  事件监听,自定义事件和自定义监听器类//  自定义事件:继承自ApplicationEvent抽象类,定义自己的构造器//  自定义监听:实现ApplicationListener接口,实现onApplicationEvent方法}

EventDemoListener1.java

package boot.example.event.listener;import boot.example.event.events.EventDemo;
import org.springframework.context.ApplicationListener;//  手动向ApplicationContext中添加监听器  不要@Component注解就能监听到
public class EventDemoListener1 implements ApplicationListener<EventDemo> {@Overridepublic void onApplicationEvent(EventDemo eventDemo) {Object msg = eventDemo.getSource();System.out.println("自定义事件监听器(MyEventListener1)收到发布的消息: " + msg);}
}

2.使用@Component注解自动监听

EventDemoListener2.java

package boot.example.event.listener;import boot.example.event.events.EventDemo;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;//  将监听器装载入spring容器
@Component
public class EventDemoListener2 implements ApplicationListener<EventDemo> {@Overridepublic void onApplicationEvent(EventDemo eventDemo) {Object msg = eventDemo.getSource();System.out.println("自定义事件监听器(MyEventListener2)收到发布的消息: " + msg);}
}

3.在application.properties中配置监听器,可以不用注解

EventDemoListener3.java

package boot.example.event.listener;import boot.example.event.events.EventDemo;
import org.springframework.context.ApplicationListener;//  在application.properties中配置监听器,可以不用注解
public class EventDemoListener3 implements ApplicationListener<EventDemo> {@Overridepublic void onApplicationEvent(EventDemo eventDemo) {Object msg = eventDemo.getSource();System.out.println("自定义事件监听器(MyEventListener3)收到发布的消息: " + msg);}}

application.properties配置

context.listener.classes=boot.example.event.listener.EventDemoListener3

4.通过@EventListener注解实现事件监听(使用最方便)

EventDemoListener4.java

package boot.example.event.listener;import boot.example.event.events.EventDemo;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;//  通过@EventListener注解实现事件监听
@Component
public class EventDemoListener4 {@EventListenerpublic void listener(EventDemo eventDemo) {Object msg = eventDemo.getSource();System.out.println("自定义事件监听器(MyEventListener4)收到发布的消息: " + msg);}}

触发监听

EventDemoController .java

package boot.example.event.controller;import boot.example.event.events.EventDemo;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping(value="/eventDemo")
public class EventDemoController {@Resourceprivate ApplicationContext applicationContext;@RequestMapping(value="/test")public String test() {applicationContext.publishEvent(new EventDemo("测试监听事件"));return "testEventDemo";}
}

控制台查看 四种方式执行默认是有先后顺序的
myw
还有一种方式 这种方式使用更方便

DemoRole.java 和 DemoUser.java

package boot.example.event.events;public class DemoRole {private Integer roleId;private String roleName;public Integer getRoleId() {return roleId;}public void setRoleId(Integer roleId) {this.roleId = roleId;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}
}
package boot.example.event.events;public class DemoUser {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

监听类EventDemoListenerMultiple.java

package boot.example.event.listener;import boot.example.event.events.DemoRole;
import boot.example.event.events.DemoUser;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;@Component
//@Service
public class EventDemoListenerMultiple {@Async@EventListener(DemoUser.class)public void listenerDemoUser(DemoUser demoUser) {String name = demoUser.getUsername();System.out.println("listenerDemoUser---"+name);}@Async@EventListener(DemoRole.class)public void listenerDemoUser(DemoRole demoRole) {String name = demoRole.getRoleName();System.out.println("listenerDemoUser---"+name);}}

监听器触发类EventDemoMultipleController.java

package boot.example.event.controller;import boot.example.event.events.DemoRole;
import boot.example.event.events.DemoUser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;@RestController
@RequestMapping(value="/eventDemo")
public class EventDemoMultipleController {@Resourceprivate ApplicationContext applicationContext;@RequestMapping(value="/demoUser")public String demoUser() {DemoUser demoUser = new DemoUser();demoUser.setUsername("admin");demoUser.setPassword("123456");applicationContext.publishEvent(demoUser);return "testEventDemo";}@Resourceprivate ApplicationEventPublisher applicationEventPublisher;@RequestMapping(value="/demoRole")public String demoRole() {DemoRole demoRole = new DemoRole();demoRole.setRoleId(4);demoRole.setRoleName("角色");applicationContext.publishEvent(demoRole);return "testEventDemo";}}

Servlet渐渐地淡出视野,也记录一下使用

CustomServletContextListener.java

package boot.example.event.config;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;/***  servelt的监听器*/
@WebListener
public class CustomServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {//  SpringBoot WEB启动间执行一次System.out.println("CustomServletContextListener--contextInitialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {//  SpringBoot WEB销毁执行System.out.println("CustomServletContextListener--contextDestroyed");}}

在启动类加上扫描注解

@ServletComponentScan("boot.example.event.config")

项目结构
myw

记录到此!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1381232.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

bat批处理文件 - win10桌面图标白标修复(图标变为白色方块)

前言 当你桌面应用变成如图所示白标时(正常运行)&#xff0c;那么这个批处理文件可以帮助你。 其中&#xff0c;IconCache.db就是图标缓存文件&#xff0c;把他删除重新构建即可。 一、新建文本文件.txt 复制以下代码 echo offtaskkill /f /im explorer.exeCD /d %userprof…

Win10系统任务栏图标显示白色方块的解决方法

时间&#xff1a;2022-07-30 问题&#xff1a;Win10系统任务栏图标显示白色方块 解决后&#xff1a; 原因&#xff1a;Windows对于图标的缓存 解决方法&#xff1a; 1.进入C盘的用户家目录下的AppData目录&#xff0c;再进入Local目录 1.1 WinR 然后运行 %localappdata% 直…

计算机桌面有去不掉的框,电脑右下角有个白色方框去不掉

电脑右下角出现了一个白色方框&#xff0c;一直去不掉&#xff1f;学习有不少用户遇到这种桌面出现白框的情况&#xff0c;在电脑桌面上非常影响正常使用。那么我们今天就一起来学习下如何去掉电脑右下角的白色方框吧。 首先&#xff0c;实用组合键“ctrlshiftEsc”唤出任务管理…

怎么解决win10电脑桌面图标右下角有黑色方块的问题!轻松修复桌面图标显示异常!【解决方案分享】

标题电脑桌面的快捷图标左下角有黑色方块 1、今天突然发现电脑桌面的快捷图标左下角有黑色方块&#xff0c;显得很难看&#xff0c;又无从下手&#xff0c;直接上图看。 2、网上找了很多方法说是 “这是缓存出错引起的&#xff0c;用下面的软件修复一下&#xff08;下载360卫…

桌面图标变白色方块,如何解决

1、桌面图标变成白色方块&#xff08;由于我的图标已修复&#xff0c;该图片为网图&#xff09; 2、桌面右击&#xff0c;在桌面新建一个文本文档&#xff0c;命名为 图标修复.bat 3、右击图片修复文本文档&#xff0c;点击编辑&#xff0c;输入一下代码并保存 代码&#xff1a…

【雕爷学编程】Arduino动手做(201)---行空板开发环境之VSCode

37款传感器与模块的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&#x…

个人空间岁末大回报活动12月26日获奖名单

个人空间岁末大回报: 动手就有C币拿!活动已于15日启动,非常感谢各位网友的大力支持和积极参与,个人空间的所有工作人员在这祝大家好运,希望你们每天都能拿到C币存入社区银行! 欢迎各位获奖者去自己的银行查看金额是否到账,如果有任何疑问都可以用【留言】【私信】等各种…

个人空间岁末大回报活动12月16日获奖名单

个人空间岁末大回报: 动手就有C币拿!活动已于15日启动,非常感谢各位网友的大力支持和积极参与,个人空间的所有工作人员在这祝大家好运,希望你们每天都能拿到C币存入社区银行! 欢迎各位获奖者去自己的银行查看金额是否到账,如果有任何疑问都可以用【留言】【私信】等各种…

马云唱京剧《空城计》,柳传志说相声:“商界春晚”大佬们真会玩(附视频)...

当马云、柳传志、曹国伟等人聚在一起&#xff0c;会发生什么&#xff1f; 在互联网大会上共论未来十年的发展趋势&#xff1f;还是在镜头前激烈探讨行业变化&#xff1f; 然而这次都不是。 他们聚在一起&#xff0c;是为了上“春晚”表演节目…… 1月10日&#xff0c;被称为被誉…

常见项目管理组织机构简介

常见项目管理组织机构简介 一、职能型组织 职能型组织如下图所示&#xff0c;一般适用于业务比较固定的企业&#xff0c;如国企或流水生产制造行业&#xff0c;这种企业工作任务一般由职能经理安排就可以&#xff0c;当然&#xff0c;现在国企也在转变&#xff0c;不一定是全职…

什么是项目管理?怎么管?(一)

前言 项目管理是团队建立共同语言的需要、保证每个项目结果的需要、积累企业过程资产必要&#xff0c;同时还是打造企业战略执行力和项目管理核心竞争力的需要。 项目管理就是要做好项目的事、做好团队的事、做好企业组织的事、做好商业的事&#xff0c;就是又要当爹又要当妈。…

高项_第18-20章组织级项目管理流程管理项目集管理

第十八章 组织级项目管理 组织级项目管理是指在组织战略的指导下&#xff0c;具体落实组织的战略行动,从业务管理、组织架构、人员配置等多个方面对组织进行项目化的管理。 组织级项目管理是组织在其内部搭建起项目组合管理、项目集管理和单项目管理的各个领域,以及在这些领域…

组织战略和项目管理

2.1.1 战略的起源于特点【选择】 在西方&#xff0c;“战略” 一词是从希腊词汇“strategos” 中衍生出来的&#xff0c;意指指挥军队的艺术和科学。 战略的定义&#xff1a; 组织为了实现其使命或长期目标&#xff0c;在不环境的互动中所展开的决策行为、 采取的行动模式或遵循…

[项目管理-14]:大规模组织的项目管理办公室PMO

作者主页(文火冰糖的硅基工坊)&#xff1a;文火冰糖&#xff08;王文兵&#xff09;的博客_文火冰糖的硅基工坊_CSDN博客 本文网址&#xff1a; 目录 第1章 什么是PMO 1.1 PMO定义 1.2 PMO的由来 1.3 PMP的职责 第2章 PMO的组织架构 1.1 架构 1.2 人员组成 1.3 类型 …

项目管理(三)组织结构与项目管理

组织结构与项目管理 职能型组织的优缺点 优点&#xff1a; 1.简单 2.对专家更易于管理&#xff0c;管理更具灵活性 3.只向一个上司汇报 4.项目人员有“家”——他们在部门里工作&#xff0c;部门给予相应的技术支持 5.员工可以不断得到提高 缺点&#xff1a; 1.项目经理没有足…

项目管理 | 项目团队组建的过程是怎样的?

项目团队的组成与发展基本遵循布鲁斯塔克曼的团队建设理论模型&#xff0c;即项目团队会经历组建、震荡、规范、成熟和解散五个阶段&#xff0c;也可称为启动、磨合、发展、成熟和收尾五个阶段&#xff0c;这两组名称只是翻译不同&#xff0c;其实它们有相互对应的关系。 项目团…

项目管理(如何进行团队管理)

团队构建与管理6步走 各项工作概述: 获取团队成员需要考虑的因素: 可用性。确认资源能否在项目所需时段内为项目所用。 成本。确认增加资源的成本是否在规定的预算内。 能力。确认团队成员是否提供了项目所需的能力。 有些选择标准对团队资源来说是独特的,包括: 经验。…

组织级项目管理

概述 组织级项目管理&#xff08; organizational Project Management ) : 是指立足于组织管理角度&#xff0c;从实现组织运营价值最大化的目标出发&#xff0c;考虑如何筹建组织级的项目管理体系&#xff0c;实现组织资源优化整合、提高项目成功率&#xff0c;并在项目立项和…

PMP——项目组织结构

项目中人员或组织的组织结构对日后的项目影响深远&#xff0c;简而言之有三种类型&#xff1a;项目型、职能型、矩阵型 一&#xff1a; 项目型:将所有的能兵强将集结在一起&#xff0c;财务部、业务部、IT管理部等的精英们脱离原有的岗位。形成一个正式的部门&#xff0c;并由…

项目的组织结构

组织结构 1.组织结构反映了&#xff1a; 公司对项目管理的重视程度、项目经理的制度化授权程度、商业性质 2.项目的组织结构 与项目有关的组织结构包括职能型、项目型、矩阵型&#xff08;弱矩阵、平衡矩阵、强矩阵&#xff09;。 1&#xff09;职能型组织 优点&#xff1…