springboot 事件监听(@EventListener实现)
2021-06-06 06:02
标签:turn initial publish apach 16px encoding print tty tar 应用:使用注解实现事件监听 相关注解 @EventListener 示例 ***************** event 层 CustomEvent CustomEventListener ***************** controller 层 HelloController 使用测试 localhost:8080/hello 转载者注: 说明: @EventListener(CustomEvent.class)表示监听CustomEvent类的信息,如果流程applicationContext.publishEvent 推送customEvent类的消息, 就会被CustomEventListener类中,标志@EventListener注解的方法所接受,并执行被注解方法的代码 pom.xml ———————————————— springboot 事件监听(@EventListener实现) 标签:turn initial publish apach 16px encoding print tty tar 原文地址:https://www.cnblogs.com/ryelqy/p/14616037.htmlspringboot 事件监听(@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 "";
}
**********************
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;
}
}
@Component
public class CustomEventListener {
@EventListener(CustomEvent.class)
public void onApplicationEvent(CustomEvent customEvent){
System.out.println("监听器接受消息:"+System.currentTimeMillis());
System.out.println("接收到的消息为:"+customEvent.getMessage());
}
}
@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";
}
}
**********************
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
接收到的消息为:你好啊
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.0modelVersion>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.4.4version>
relativePath/>
parent>
groupId>com.examplegroupId>
artifactId>demoartifactId>
version>0.0.1-SNAPSHOTversion>
name>demoname>
description>Demo project for Spring Bootdescription>
properties>
java.version>11java.version>
swagger.version>2.9.2swagger.version>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger2artifactId>
version>${swagger.version}version>
dependency>
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger-uiartifactId>
version>${swagger.version}version>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
scope>testscope>
dependency>
dependencies>
build>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
版权声明:本文为CSDN博主「o_瓜田李下_o」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43931625/article/details/107135241
文章标题:springboot 事件监听(@EventListener实现)
文章链接:http://soscw.com/index.php/essay/91143.html