springboot 事件监听(ApplicationListener实现)

    技术2025-12-12  18


    springboot 事件监听(ApplicationListener实现)

     

    事件:实现bean与bean之间的通信,当一个bean处理完后,另一个bean做相应的处理

     

     

    *********************

    相关类与接口

     

    ApplicationEvent

    public abstract class ApplicationEvent extends EventObject { private static final long serialVersionUID = 7099057708183571937L; private final long timestamp = System.currentTimeMillis(); public ApplicationEvent(Object source) { super(source); } public final long getTimestamp() { return this.timestamp; } }

     

     

    ApplicationEventPublisher:事件发布

    @FunctionalInterface public interface ApplicationEventPublisher { default void publishEvent(ApplicationEvent event) { this.publishEvent((Object)event); } void publishEvent(Object var1); }

     

    ApplicationContext:该接口实现了ApplicationEventPublisher

    public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver { @Nullable String getId(); String getApplicationName(); String getDisplayName(); long getStartupDate(); @Nullable ApplicationContext getParent(); AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException; }

     

     

    ApplicationListener:事件监听

    @FunctionalInterface public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { void onApplicationEvent(E var1); }

     

    EventListener

    public interface 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 implements ApplicationListener<CustomEvent> { @Override 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:02:34.267 INFO 22504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-07-05 10:02:34.273 INFO 22504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms 事件开始发布消息:1593914554286 监听器接受消息:1593914554286 接收到事件消息:你好啊

     

     

    Processed: 0.026, SQL: 9