springboot 事件监听(@EventListener实现)
应用:使用注解实现事件监听
**********************
相关注解
@EventListener
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EventListener { @AliasFor("classes") Class<?>[] value() default {}; //监听的类 @AliasFor("value") Class<?>[] classes() default {}; String condition() default ""; }
**********************
示例
*****************
event 层
CustomEvent
public class CustomEvent extends ApplicationEvent { private String message; public CustomEvent(Object source,String message){ super(source); this.message=message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
CustomEventListener
@Component public class CustomEventListener { @EventListener(CustomEvent.class) public void onApplicationEvent(CustomEvent customEvent){ System.out.println("监听器接受消息:"+System.currentTimeMillis()); System.out.println("接收到的消息为:"+customEvent.getMessage()); } }
*****************
controller 层
HelloController
@RestController public class HelloController { @Resource private ApplicationContext applicationContext; @RequestMapping("/hello") public String hello(){ System.out.println("事件开始发布消息:"+System.currentTimeMillis()); applicationContext.publishEvent(new CustomEvent(this,"你好啊")); return "success"; } }
**********************
使用测试
localhost:8080/hello
2020-07-05 10:20:14.512 INFO 18472 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-07-05 10:20:14.517 INFO 18472 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms 事件开始发布消息:1593915614552 监听器接受消息:1593915614553 接收到的消息为:你好啊
转载者注:
说明:
@EventListener(CustomEvent.class)表示监听CustomEvent类的信息,如果流程applicationContext.publishEvent 推送customEvent类的消息,
就会被CustomEventListener类中,标志@EventListener注解的方法所接受,并执行被注解方法的代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <swagger.version>2.9.2</swagger.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
————————————————
版权声明:本文为CSDN博主「o_瓜田李下_o」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43931625/article/details/107135241