java springboot监听事件和处理事件

在Spring Boot中监听和处理事件

在Spring Boot中,事件机制是一个非常有用的功能,它允许开发人员在应用程序中发布和监听事件,以解耦组件和提高系统的可维护性。下面将详细介绍如何在Spring Boot中监听和处理事件,包括创建自定义事件、发布事件和监听事件。

1. 创建自定义事件

首先,需要创建一个自定义事件类,该类需要继承 ApplicationEvent类。

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    private String message;

    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
​
 
 

2. 发布事件

在需要发布事件的地方,可以通过 ApplicationEventPublisher来发布自定义事件。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class CustomEventPublisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishEvent(final String message) {
        CustomEvent customEvent = new CustomEvent(this, message);
        applicationEventPublisher.publishEvent(customEvent);
    }
}
​
 
 

3. 监听事件

为了监听自定义事件,需要创建一个监听器类,并使用 @EventListener注解来标记监听方法。

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

@Component
public class CustomEventListener {

    @EventListener
    public void handleCustomEvent(CustomEvent event) {
        System.out.println("Received custom event - " + event.getMessage());
    }
}
​
 
 

4. 示例:整合所有组件

下面是一个完整的示例,展示如何在Spring Boot中使用自定义事件。

4.1 创建Spring Boot应用

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

@SpringBootApplication
public class EventDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(EventDemoApplication.class, args);
    }
}
​
 
 

4.2 创建事件类

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    private String message;

    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
​
 
 

4.3 创建事件发布者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class CustomEventPublisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishEvent(final String message) {
        CustomEvent customEvent = new CustomEvent(this, message);
        applicationEventPublisher.publishEvent(customEvent);
    }
}
​
 
 

4.4 创建事件监听器

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

@Component
public class CustomEventListener {

    @EventListener
    public void handleCustomEvent(CustomEvent event) {
        System.out.println("Received custom event - " + event.getMessage());
    }
}
​
 
 

4.5 发布事件

在某个控制器或服务类中调用发布事件的方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class EventController {

    @Autowired
    private CustomEventPublisher eventPublisher;

    @GetMapping("/publish")
    public String publish() {
        eventPublisher.publishEvent("Hello, World!");
        return "Event Published!";
    }
}
posted @ 2025-05-12 10:41  我是一只小小鸟~  阅读(37)  评论(0)    收藏  举报