Spring 事件机制
来源:互联网 发布:samba端口号作用 编辑:程序博客网 时间:2024/06/05 01:58
完成Spring事件需要简单的三步
- 创建DataEvent,继承自ApplicationEvents
- 创建DataEventPublisher类,实现ApplicationEventPublisherAware接口
- 创建DataEventListener类,实现ApplicationListener接口
DataEvent类
import lombok.Data;@Datapublic class DataEvent extends ApplicationEvent { private String message; public DataEvent(Object source, String message) { super(source); this.message = message; }}
创建DataEventPublisher类接口
public interface DataEventPublisher { void publishDataSuccess(); void publishDataFailure();}
创建Publisher实现类
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.context.ApplicationEventPublisher;import org.springframework.context.ApplicationEventPublisherAware;@Componentpublic class DefaultDataEventPublisher implements DataEventPublisher, ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; @Override public void publishDataSuccess() { if (applicationEventPublisher != null) { applicationEventPublisher.publishEvent(new DataEvent(this, "Success")); } } @Override public void publishDataFailure() { if (applicationEventPublisher != null) { applicationEventPublisher.publishEvent(new DataEvent(this, "Failure")); } } @Override public void setApplicationEventPublisher( ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; }}
创建DataEventListener实现类
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;@Componentpublic class DataEventListener implements ApplicationListener<DataEvent> { private final static Logger LOGGER = LoggerFactory.getLogger(DataEventListener.class); @Override public void onApplicationEvent(DataEvent event) { LOGGER.info("Received event message: {}", event.getMessage()); }}
编写测试类
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class DataPublisherTest { @Autowired private DefaultDataEventPublisher publisher; @Test public void publiserTest() throws Exception { publisher.publishDataSuccess(); }}
将会在输出的命令行中看到信息
“Received event message: Success”;
阅读全文
0 0
- spring中事件机制
- spring的事件机制
- Spring事件机制
- Spring ApplicationContext事件机制
- Spring事件机制
- spring 事件机制
- spring中的事件机制
- 二,Spring事件机制
- spring的事件机制
- Spring-ApplicationContext事件机制
- Spring 事件机制
- Spring ApplicationContext事件机制
- spring 事件机制demo
- Spring事件机制项目应用
- spring容器的事件机制
- Spring---ApplicationContext的事件机制
- Spring的事件发布机制
- 如何自定义事件(Spring事件机制浅谈)
- web.xml中的url-pattern标签/和/*有什么区别
- custom python matlabplot
- 日程的增删改查
- 建站也有认证了?你会去考吗
- HDU-2680 Choose the best route
- Spring 事件机制
- 8.10实战感悟
- VC DLL转换C++builder Lib
- 《大话数据结构》 第七章 图
- 红黑树笔记
- Java实现简单的网络爬虫(一)
- typename基本使用方法
- NVIDIA Jetson TX2内核添加CP210x串口驱动
- 关于"Autoresizing Mask"