标签:source 技术 extend constant eve dex 方便 sts nbsp
写在前面----------------------------------
命运多舛,痴迷淡然
不知下一步该往哪里走,现在应该是我的迷茫期。。。
加油,快点走出去!!!
聪明的网友们,你们有没有迷茫的时候,你们又是怎么度过的呢?
通过上面的笔记我们可以了解到spring的事件的监听,但是这个都是spring中定义好的,那么我们如何自定义事件,监听,以及发布呢?、
那么事件的创建,我们是实现spring的事件接口
Spring的事件对象
package cn.cutter.start.event; import org.springframework.context.ApplicationEvent; import cn.cutter.start.event.constant.MethodExecutionStatusEnum; /** * * Title: MethodExecutionEvent.java * Description: 定义一个自定义的spring事件对象 * @author xiaof * @date 2018年3月28日 * @version 1.0 * */ public class MethodExecutionEvent extends ApplicationEvent { /** * */ private static final long serialVersionUID = 2211772210416245698L; private String methodName; private MethodExecutionStatusEnum methodExecutionStatusEnum; /** * 构造函数是,继承的类实现的 * @param source */ public MethodExecutionEvent(Object source) { super(source); } public MethodExecutionEvent(Object source, String methodName, MethodExecutionStatusEnum methodExecutionStatusEnum) { super(source); this.methodName = methodName; this.methodExecutionStatusEnum = methodExecutionStatusEnum; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public MethodExecutionStatusEnum getMethodExecutionStatusEnum() { return methodExecutionStatusEnum; } public void setMethodExecutionStatusEnum(MethodExecutionStatusEnum methodExecutionStatusEnum) { this.methodExecutionStatusEnum = methodExecutionStatusEnum; } }
事件的监听
package cn.cutter.start.event; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import cn.cutter.start.event.constant.MethodExecutionStatusEnum; /** * * Title: MethodExecutionEventListener.java * Description: spring事件监听类型 * @author xiaof * @date 2018年4月1日 * @version 1.0 * */ @Component //一定要添加进入spring的容器,才能进行监听 public class MethodExecutionEventListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { if(event instanceof MethodExecutionEvent) { MethodExecutionEvent methodExecutionEvent = (MethodExecutionEvent) event; //执行处理逻辑 System.out.println("执行自定义事件的相应操作"); if(methodExecutionEvent.getMethodExecutionStatusEnum().equals(MethodExecutionStatusEnum.BEGIN)) { //方法开始方式 System.out.println("begin 开始执行前置事件----" + methodExecutionEvent.getMethodName()); } else { System.out.println("end 开始执行后置事件----" + methodExecutionEvent.getMethodName()); } } } }
事件的发布对象
package cn.cutter.start.event; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.stereotype.Component; import cn.cutter.start.event.constant.MethodExecutionStatusEnum; /** * * Title: MethodExeuctionEventPublisher.java * Description: 这里实现ApplicationEventPublisherAware,方便对applicationEventPublisher的注入 * @author xiaof * @date 2018年4月1日 * @version 1.0 * */ @Component public class MethodExeuctionEventPublisher implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } public void methodToMonitor() { //这个方法就是对事件的发布操作 //发起开始事件并发布,发起后置事件并结束 MethodExecutionEvent beginEvnt = new MethodExecutionEvent(this, "cn.cutter.start.event methodToMonitor方法触发事件", MethodExecutionStatusEnum.BEGIN); MethodExecutionEvent endEvnt = new MethodExecutionEvent(this, "cn.cutter.start.event methodToMonitor方法触发事件", MethodExecutionStatusEnum.END); //发布 this.applicationEventPublisher.publishEvent(beginEvnt); // System.out.println("进行 methodToMonitor 业务方法本体"); //发布 this.applicationEventPublisher.publishEvent(endEvnt); } }
测试方式:
@Test public void testSpringEvent() { ApplicationContext ctx = this.before(); cn.cutter.start.event.MethodExeuctionEventPublisher methodExeuctionEventPublisher = (cn.cutter.start.event.MethodExeuctionEventPublisher) ctx.getBean("methodExeuctionEventPublisher"); methodExeuctionEventPublisher.methodToMonitor(); }
操作结果:
标签:source 技术 extend constant eve dex 方便 sts nbsp
原文地址:https://www.cnblogs.com/cutter-point/p/8886193.html