在Spring Boot中,你可以使用监听器来响应特定的事件。这些事件可以是Spring Boot应用生命周期中的某个阶段(如启动、关闭等),也可以是你自定义的业务事件。

1. 创建一个监听器

创建一个监听器有两种方法:实现ApplicationListener接口或使用@EventListener注解。

实现ApplicationListener接口

创建一个新的类并实现ApplicationListener接口,传入你想要监听的事件类型作为泛型参数。

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

@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("Application is ready!");
    }
}

在这个例子中,我们创建了一个名为MyApplicationListener的监听器,它会在Spring Boot应用准备好之后执行一些操作(打印一条消息)。

使用@EventListener注解

你也可以通过在方法上添加@EventListener注解来创建一个监听器:

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

@Component
public class MyEventListener {

    @EventListener
    public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
        System.out.println("Application is ready!");
    }
}

2. 自定义事件

在 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;
    }
}

在这个例子中,我们创建了一个名为 CustomEvent 的自定义事件类,它包含了一个字符串类型的 message 属性,用于存储事件的相关信息。

  1. 创建一个监听器类,实现 ApplicationListener 接口,并指定要监听的事件类型(这里是 CustomEvent):
import org.springframework.context.ApplicationListener;

public class CustomEventListener implements ApplicationListener<CustomEvent> {

    @Override
    public void onApplicationEvent(CustomEvent event) {
        System.out.println("Received custom event: " + event.getMessage());
    }
}
  1. 注册监听器到 Spring 容器。你可以使用自动扫描、手动注册 Bean 或使用 @EventListener 注解来完成这一任务。
  2. 发布自定义事件:现在你已经创建了自定义事件和监听器,可以使用 ApplicationContext 来发布你的自定义事件:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EventPublisher {
 @Autowired
    private final ApplicationContext context;

   
    public EventPublisher(ApplicationContext context) {
        this.context = context;
    }

    public void publishCustomEvent(String message) {
        context.publishEvent(new CustomEvent(this, message));
    }
}

在这个示例中,我们创建了一个 EventPublisher 类,它有一个注入的 ApplicationContext。当调用 publishCustomEvent() 方法时,会发布一个新的 CustomEvent,并将消息传递给监听器。

  1. 最后,在需要的地方调用 EventPublisher 类的 publishCustomEvent() 方法来触发事件:
@Autowired
private EventPublisher eventPublisher;

// ...

eventPublisher.publishCustomEvent("Hello from a custom event!");

这样,每当 publishCustomEvent() 被调用时,你的自定义监听器就会接收到事件并执行相应的操作。

3.springboot使用定时任务

这种方式很简单,主要就是先@EnableScheduling开启定时任务功能,然后在相应的方法上添加@Scheduled()中间写上相应的cron表达式即可。示例如下:

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定时任务注解+cron表达式
    public void testScheduleTask() {
        System.out.println("执行定时任务" + LocalDateTime.now());
    }
}