当前位置: 首页 > news >正文

株洲建设网站制作网络怎么推广自己的产品

株洲建设网站制作,网络怎么推广自己的产品,做网站的公司哪家最好,分宜网站建设前言 讲到事件监听#xff0c;这里我们说下自定义事件和自定义监听器类的实现方式#xff1a; 自定义事件#xff1a;继承自ApplicationEvent抽象类#xff0c;然后定义自己的构造器 自定义监听#xff1a;实现ApplicationListener接口#xff0c;然后实现onApplication…前言 讲到事件监听这里我们说下自定义事件和自定义监听器类的实现方式 自定义事件继承自ApplicationEvent抽象类然后定义自己的构造器 自定义监听实现ApplicationListener接口然后实现onApplicationEvent方法 下面讲下4种事件监听的具体实现 1.手工向ApplicationContext中添加监听器 首先创建MyListener1类 public class MyListener1 implements ApplicationListenerMyEvent{Logger logger Logger.getLogger(MyListener1.class);public void onApplicationEvent(MyEvent event){logger.info(String.format(%s监听到事件源%s., MyListener1.class.getName(), event.getSource()));} }然后在springboot应用启动类中获取ConfigurableApplicationContext上下文装载监听 SpringBootApplication public class LisenterApplication{public static void main(String[] args){ConfigurableApplicationContext context SpringApplication.run(LisenterApplication.class, args);//装载监听context.addApplicationListener(new MyListener1());} }2.将监听器装载入spring容器 创建MyListener2类并使用Component注解将该类装载入spring容器中 Component public class MyListener2 implements ApplicationListenerMyEvent{Logger logger Logger.getLogger(MyListener2.class);public void onApplicationEvent(MyEvent event) {logger.info(String.format(%s监听到事件源%s., MyListener2.class.getName(), event.getSource()));} }3.在application.properties中配置监听器 首先创建MyListener3类 public class MyListener3 implements ApplicationListenerMyEvent{Logger logger Logger.getLogger(MyListener3.class);public void onApplicationEvent(MyEvent event){logger.info(String.format(%s监听到事件源%s., MyListener3.class.getName(), event.getSource()));} }然后在application.properties中配置监听 context.listener.classescom.listener.MyListener34.通过EventListener注解实现事件监听 创建MyListener4类该类无需实现ApplicationListener接口使用EventListener装饰具体方法 Component public class MyListener4{Logger logger Logger.getLogger(MyListener4.class);EventListenerpublic void listener(MyEvent event){logger.info(String.format(%s监听到事件源%s., MyListener4.class.getName(), event.getSource()));} }自定义事件代码如下 public class MyEvent extends ApplicationEvent{public MyEvent(Object source){super(source);} }进行测试(在启动类中加入发布事件的逻辑) SpringBootApplication public class LisenterApplication{Autowiredprivate ApplicationEventPublisher applicationEventPublisher;Autowiredprivate ApplicationContext applicationContext;public static void main(String[] args){ConfigurableApplicationContext context SpringApplication.run(LisenterApplication.class, args);//装载事件context.addApplicationListener(new MyListener1());//发布事件方式1context.publishEvent(new MyEvent(测试事件.));//发布事件方式2applicationEventPublisher.publishEvent(new MyEvent(测试事件.));//发布事件方式3applicationContext.publishEvent(new MyEvent(测试事件.));} }启动后日志打印如下 2022-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener3 : com.listener.MyListener3监听到事件源测试事件.. 2022-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener4 : com.listener.MyListener4监听到事件源测试事件.. 2022-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener2 : com.listener.MyListener2监听到事件源测试事件.. 2022-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener1 : com.listener.MyListener1监听到事件源测试事件..由日志打印可以看出SpringBoot四种事件的实现方式监听是有序的
http://www.zqtcl.cn/news/942341/

相关文章:

  • dtu网站开发赣县网站制作
  • 东莞旅游网站建设微网站怎么做
  • 网站怎么没有排名做义工旅行有哪些网站
  • 阳江房地产信息网官方网站创业网站开发要多少钱
  • 工业设计招聘信息网站常用的seo网站优化排名
  • 温岭市建设规划局网站网站规划与建设ppt
  • 龙岩网站建设较好的公司做网站销售的换工作
  • 潞城建设局网站建设网站服务器自营方式的特点
  • 西安网站seo公司东莞市专注网站建设怎么样
  • dede游戏网站模板如何做盆栽蔬菜网站
  • 江都建设网站网站开发技术介绍
  • 网站介绍视频怎么做网站建设优化服务
  • 可以左右滑动的网站有口碑的盐城网站建设
  • 360报危险网站注册界面设计
  • 不用淘宝客api如何做网站北京移动官网网站建设
  • 手表哪个网站做的好河北网站备案流程
  • 凡科做的网站推效果网站做seo第一步
  • 建设在线观看视频网站免费企业网站建设免费
  • 网站开发需要后台吗哪家建站公司好
  • 个人建设网站论文网站视频怎么做的
  • 不同类型的购物网站汉川网站建设
  • 网站开发需求文档范文广州公司网站托管
  • 网站制作公司官网首页撸撸撸做最好的导航网站
  • 网站建设毕业设计综述centos 安装wordpress lnmp
  • 济宁专业做网站网站建设中 html
  • 中国排名高的购物网站最新发布的手机2022
  • 备案的网站名与公司名称出国用哪个地图app好
  • 网站建设工作室图片文章资讯类网站
  • 深圳自助建站系统网站题目有哪些
  • 郑州做网站kuihuakeji软文发布的平台与板块