spring 容器的自带事件以及 自定义事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_38151745/article/details/83347867

ApplicationEvent

spring 的事件是为bean与bean 之间的消息通信提供了支持,当一个bean 处理完一个任务后,希望另外一个bean 知道并能够做出相应的处理,这时需要另外一个bean监听当前bean 所发送的事件。
ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之间的解耦,提高可扩展性以及可维护性。事件发布者并不需要考虑谁去监听,监听具体的实现内容是什么,发布者的工作只是为了发布事件而已。
ApplicationEvent 可以对多种事件进行监听,下面是一些简单的事件监听

@Component
public class ApplicationConfig implements ApplicationListener<ApplicationEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfig.class);

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof ContextStartedEvent) {
            LOGGER.info("spring  happen start work");
        } else if (applicationEvent instanceof ContextRefreshedEvent) {
            //一般来说,可以使用ContextRefreshedEvent 在程序启动的时候运行一些需要程序运行的方法,例如文本的读取等
            if (((ContextRefreshedEvent) applicationEvent).getApplicationContext().getParent() == null) {
                LOGGER.info("spring happen  refresh event ");
            }
        } else if (applicationEvent instanceof ContextStoppedEvent) {
            LOGGER.info("spring happen stop event ");
        } else if (applicationEvent instanceof ContextClosedEvent) {
            LOGGER.info("spring happen close  event ");
        } else {
            LOGGER.info("spring  happen other  event " + applicationEvent.getClass().getSimpleName());
        }
    }
}

ContextRefreshedEvent 事件需要特别注意,他可以支持在程序启动的时候执行需要执行的方法

自定义事件

spring 的事件需要遵循以下流程

  1. 继承ApplicationEvent 实现自定义事件
/**
 * Created by Sean on 2018/7/19
 *
 * @author Sean
 */
public class UserEvent extends ApplicationEvent {
    private String msg;
    public UserEvent(Object source ,String msg) {
        super(source);
        this.msg=msg;
    }

    public String getMsg() {
        return msg;
    }
}
  1. 定义事件监听器 实现applicationListener
    (1.实现applicationListener ,并指定监听事件的类型
    1. 使用onapplicationevent 方法对消息进行接收处理 )
/**
 * Created by Sean on 2018/7/19
 *
 * @author Sean
 */
@Component
public class UserEventListener implements ApplicationListener<UserEvent> {
    private static final Logger LOGGER= LoggerFactory.getLogger(UserEventListener.class);
    @Override
    public void onApplicationEvent(UserEvent userEvent) {
        String msg = userEvent.getMsg();
        LOGGER.info("User Event Listener get Msg form UserEvent is "+msg);
    }
}
  1. 使用容器发布事件
    1.注入 applicationcontext 来发布事件
    2.使用applicationcontext的publishevent 方法来发布事件
@Component
public class UserEventPublisher {
    @Autowired
    private ApplicationContext applicationContext;
    public void publish(String msg){
        applicationContext.publishEvent(new UserEvent(this,msg));
    }

}
  1. 发布事件
@RestController
public class UserEventController {
    @Autowired
    private UserEventPublisher userEventPublisher;
    @RequestMapping("publish")
    public String publish(){
        userEventPublisher.publish("hello user");
        return "publish event";
    }
}

github url:springbootcustomerevent

猜你喜欢

转载自blog.csdn.net/github_38151745/article/details/83347867