springboot 事件監聽(@EventListener實現)


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM