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

深圳做网站比较写文章的网站

深圳做网站比较,写文章的网站,整站网站模板,网站底部公司是什么样的记录一下SpringBoot自定义事件监听器的使用方法 案例源码:SpringBoot使用自定义事件监听器的demo 使用的SpringBoot2.0.x版本 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><…

记录一下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://www.yayakq.cn/news/512035/

相关文章:

  • 昆明网站建设一条龙服务学历提升专升本
  • 那些网站是做俄罗斯鞋子做一个网站一般要多少钱
  • 高级网站建设wordpress 教程
  • 做网站开店微网站开发案例
  • 云南网站开发建设志鸿优化网官网
  • 网站和软件有什么区别广州注册公司流程及费用
  • 如何预览做好的网站广州seo公司推荐
  • 网站摄影设计定制衣服的app
  • asp网站建设教案h5网站制作公司
  • 潍坊网站建设推广公司游戏开科技怎么开
  • 外贸网站的域名wordpress安全防护
  • 1717做网站专业做网站路桥
  • 怎么屏蔽ip段访问网站电脑网页开发
  • 网站项目开发收费标准seo怎么搞
  • 什么是网站根目录网站开发 密码
  • 网站开发流程数据库自己怎么优化网站排名
  • 鲜花商城网站设计建筑学不会画画影响大吗
  • 安徽省建设造价管理协会网站全屏网站怎么做的
  • 莆田企业自助建站系统网站建设编写代码出错
  • 响应式电商网站长沙网页制作
  • 做网站业务员提成几个点招聘系统推广哪家好
  • 扬中零壹网站建设lamp网站开发黄金组...
  • 公司网站建设情况报告2008 .net 网站 目录 权限管理
  • asp化妆品网站 后台东戴河网站建设
  • 网站开发公司人员配置推销产品的万能句子
  • 网站建设费用皆选网络求推荐好玩的网页游戏
  • 网店网站建设云商城是合法的吗
  • 什么网站可以免费做兼职crm客户管理系统简历
  • 威海经区建设局网站wordpress搬站流程
  • 哈尔滨市做淘宝的网站宜宾网站建设略奥网络