(010)Spring Boot之事件監聽

  本篇記錄一下springboot監聽事件的四種方式。整體來講,springboot監聽事件包含四個步驟java

  (1)自定義要監聽的事件web

  (2)自定義監聽器spring

  (3)使spring容器獲取到監聽器,這裏有三種方式,後面會依次說到apache

  (4)發佈事件springboot

  下面是詳細步驟:app

  pom.xmlmaven

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edu.spring</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.6.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <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>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

  MyApplicationEvent.java自定義事件,繼承ApplicationEventide

package com.edu.spring.springboot;

import org.springframework.context.ApplicationEvent;

public class MyApplicationEvent extends ApplicationEvent {

    private static final long serialVersionUID = 1L;

    public MyApplicationEvent(Object source) {
        super(source);
    }
}
View Code

  MyAppilcationListener.java實現接口ApplicationListener,接收自定義事件的泛型spring-boot

package com.edu.spring.springboot;

import org.springframework.context.ApplicationListener;

public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {

    public void onApplicationEvent(MyApplicationEvent event) {
    System.out.println("接收到事件:"+event.getClass());    
    }

}
View Code

  App.java發佈事件,這裏添加監聽器的方式是SpringApplication的addListeners方法ui

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication app=new SpringApplication(App.class);
        app.addListeners(new MyAppilcationListener());//添加監聽
        ConfigurableApplicationContext context=app.run(args);
        context.publishEvent(new MyApplicationEvent(new Object()));//發佈事件
        context.close();
    }
}
View Code

  運行結果以下:

   第二種添加監聽器的方式,在自定義的監聽器上添加@Component,以下:

  MyAppilcationListener.java

package com.edu.spring.springboot;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {

    public void onApplicationEvent(MyApplicationEvent event) {
    System.out.println("接收到事件:"+event.getClass());    
    }

}
View Code

  App.java,如今不須要調用SpringApplication的addListeners方法,以下:

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication app=new SpringApplication(App.class);
        ConfigurableApplicationContext context=app.run(args);
        context.publishEvent(new MyApplicationEvent(new Object()));//發佈事件
        context.close();
    }
}
View Code

  第三種添加監聽器的方式,在application.properties配置文件中添加context.listener.classes屬性,以下

  application.properties

context.listener.classes=com.edu.spring.springboot.MyAppilcationListener
View Code

  MyAppilcationListener.java,不須要添加註解,以下:

package com.edu.spring.springboot;

import org.springframework.context.ApplicationListener;

public class MyAppilcationListener implements ApplicationListener<MyApplicationEvent> {

    public void onApplicationEvent(MyApplicationEvent event) {
    System.out.println("接收到事件:"+event.getClass());    
    }

}
View Code

  App.java,如今不須要調用SpringApplication的addListeners方法,以下:

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication app=new SpringApplication(App.class);
        ConfigurableApplicationContext context=app.run(args);
        context.publishEvent(new MyApplicationEvent(new Object()));//發佈事件
        context.close();
    }
}
View Code

  運行結果以下:

  第四種添加監聽器的方式,使用@EventListener註解,以下:

  MyEventHandle.java

package com.edu.spring.springboot;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventHandle {

    @EventListener
    public void event(MyApplicationEvent event) {
        System.out.println("MyEventHandle接收到事件:"+event.getClass());    
    }
}
View Code

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication app=new SpringApplication(App.class);
        ConfigurableApplicationContext context=app.run(args);
        context.publishEvent(new MyApplicationEvent(new Object()));//發佈事件
        context.close();
    }
}
View Code

  運行結果以下:

   很明顯,第四種方式最簡潔,既不用實現ApplicationListener接口,也不用調用SpringApplication的addListeners方法。

  spring自定義了許多事件,以下圖:

   使用這些事件的時候不用發佈,下面拿ContextStoppedEvent舉例,該事件在調用ConfigurableApplicationContext的stop方法時觸發。

  MyEventHandle.java,能夠寫多個事件,只要在方法上添加@EventListener便可

package com.edu.spring.springboot;

import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventHandle {
    
    @EventListener
    public void test(ContextStoppedEvent event){
        System.out.println("ContextStoppedEvent接收到事件:"+event.getClass());
    }
    
}
View Code

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication app=new SpringApplication(App.class);
        ConfigurableApplicationContext context=app.run(args);
        context.stop();
    }
}
View Code

  運行結果以下:

相關文章
相關標籤/搜索