spring boot 事件监听
记录工作中的点点滴滴。。。。。。
maven依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
package com.tsing.event; import org.springframework.context.ApplicationEvent; public class MyApplicationEvent extends ApplicationEvent{ /** * */ private static final long serialVersionUID = 1L; public MyApplicationEvent(Object source) { super(source); } }
将监听器纳入到 spring 容器的方式:
1. 代码: application.addListeners(new MyApplicationListener()); + 实现 ApplicationListener接口
2. @Component注解 + 实现 ApplicationListener接口
3. 配置文件的方式 + 实现 ApplicationListener接口
4. @Component注解 + @EventListener注解
方式1
package com.tsing.event; import org.springframework.context.ApplicationListener; public class MyApplicationListener implements ApplicationListener<MyApplicationEvent>{ @Override public void onApplicationEvent(MyApplicationEvent event) { System.out.println("MyApplicationListener接收到事件:" + event.getClass()); } }
package com.tsing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.tsing.event.MyApplicationEvent; import com.tsing.event.MyApplicationListener; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication sp = new SpringApplication(Application.class); // 配置事件监听器 sp.addListeners(new MyApplicationListener()); ConfigurableApplicationContext context = sp.run(args); // 发布事件 context.publishEvent(new MyApplicationEvent(new Object())); //context.close(); } }
方式2
package com.tsing.event; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * 将监听器纳入到 spring 容器的方式: * 1. 代码: application.addListeners(new MyApplicationListener()); + 实现 ApplicationListener接口 * 2. @Component注解 + 实现 ApplicationListener接口 * 3. 配置文件的方式 + 实现 ApplicationListener接口 * 4. @Component注解 + @EventListener注解 * */ @Component public class MyApplicationListenerByAnnotation implements ApplicationListener<MyApplicationEvent>{ @Override public void onApplicationEvent(MyApplicationEvent event) { System.out.println("MyApplicationListenerByAnnotation接收到事件:" + event.getClass()); } }
方式3
package com.tsing.event; import org.springframework.context.ApplicationListener; /** * 配置文件实现的方式 * */ public class MyApplicationListenerByCF implements ApplicationListener<MyApplicationEvent>{ @Override public void onApplicationEvent(MyApplicationEvent event) { System.out.println("MyApplicationListenerByCF接收到事件:" + event.getClass()); } }
配置文件application.properties
context.listener.classes=com.tsing.event.MyApplicationListenerByCF
方式4
package com.tsing.event; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class MyEventHandle { @EventListener public void event1(MyApplicationEvent event) { System.out.println("事件1==>>" + event.getClass()); } @EventListener public void event2(MyApplicationEvent event) { System.out.println("事件2==>>" + event.getClass()); } @EventListener public void event3(MyApplicationEvent event) { System.out.println("事件3==>>" + event.getClass()); } }