springboot 事件监听(ApplicationListener实现)


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
接收到事件消息:你好啊

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值