Java 开发中有些逻辑是这样的,完成了A操做,再继续B操做,在继续C操做。这么描述好像有点不清楚。打个比方把,你吃晚饭,通知你老婆(女朋友)来收碗筷,而后通知你的线上兄弟告诉他们你回来了准备开黑。至于你老婆(女朋友)来不来收拾无所谓,反正你告诉她了。至于你兄弟你也是通知他们,人家也不必定组你,万一他们正在跟一个一拖三的carry大佬玩的正起劲儿呢。java
吃晚饭就是一个所谓的事件。触发了随后的两个操做,他们只存在因果关系。可是它们互不干扰,各自为政。一个完整的事件由 事件源、事件发布、事件监听 组成。 接下来咱们聊聊 Spring 中的事件。git
Spring 框架中使用了大量的事件机制,好比 Spring Boot 的启动。方便起见咱们新建一个 Spring Boot 工程。而后跟着我一步步的来进行事件的操做。spring
org.springframework.context.ApplicationEvent
来编写事件。时间里定义好事件推送到监听器须要执行的方法,固然也能够在监听器里写触发逻辑。咱们来声明一下:package cn.felord.boot.event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/** * 吃饭事件 * * @author dax * @since 2019 /7/8 21:54 */
@Slf4j
public class EatEvent extends ApplicationEvent {
private Boolean eatFinished;
/** * Instantiates a new Eat event. * * @param eatFinished 吃饭是否完成的信号 这里也能够传递其余资源 */
public EatEvent(Boolean eatFinished) {
super(eatFinished);
this.eatFinished = eatFinished;
}
/** * 这里会由对应监听器{@link ApplicationListener<EatEvent>} 执行 * * 叫女朋友收拾碗筷. */
public void callGirlFriend() {
log.info("亲爱的! 我吃完饭了,来收拾收拾吧");
}
/** * 这里会由对应监听器{@link ApplicationListener<EatEvent>} 执行 * 呼叫兄弟开黑. */
public void callBrothers() {
log.info("兄弟们! 我吃完饭了,带我开黑");
}
/** * 吃晚饭的信号. * * @return the boolean */
public Boolean isEatFinished() {
return this.eatFinished;
}
}
复制代码
发布事件经过实现事件发布接口 org.springframework.context.ApplicationEventPublisher
或者其门面接口 org.springframework.context.ApplicationEventPublisherAware
, 推荐门面接口,里面要定义一个主动推送事件的方法以下面的 refreshEvent
方法,实际代理了 ApplicationEventPublisher
执行其 publishEvent
方法:app
package cn.felord.boot.event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
/** * 发布事件 发布事件经过实现 事件发布接口 {@link ApplicationEventPublisher} * 或者经过门面接口{@link ApplicationEventPublisherAware} * 推荐按照下面的实现方式,并且该类须要注册为spring bean * * @author dax * @since 2019 /7/8 22:04 */
@Slf4j
public class EatEventPublisherAware implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
private ApplicationEvent eatEvent;
public EatEventPublisherAware(ApplicationEvent eatEvent) {
this.eatEvent = eatEvent;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
/** * 发送事件动做 事件的动做须要主动触发 调用此方法进行事件触发 * 代理{@link ApplicationEventPublisher#publishEvent(ApplicationEvent)} */
public void refreshEvent() {
log.info("发送事件中……");
this.applicationEventPublisher.publishEvent(eatEvent);
}
}
复制代码
事件监听用来监听事件以触发相关的逻辑。经过实现 org.springframework.context.ApplicationListener<E extends ApplicationEvent>
来实现事件的监听。特别注意泛型E,若是不指定事件将能够接收任何事件,尽可能职责单一。框架
package cn.felord.boot.event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
/** * {@link EatEvent}事件的专属事件监听器 * @author dax * @since 2019/7/8 22:11 */
@Slf4j
public class EatEventListener implements ApplicationListener<EatEvent> {
@Override
public void onApplicationEvent(EatEvent eatEvent) {
//若是吃完饭了
if (eatEvent.isEatFinished()) {
eatEvent.callGirlFriend();
log.error("来自母老虎的咆哮:滚犊子");
eatEvent.callBrothers();
log.error("太晚了,咱们已经满了,明天带你");
log.info("仍是关注一下 【码农小胖哥】 学习点新知识吧");
}
}
}
复制代码
将上面三个类注入 Spring
容器中,这里咱们采用了 JavaConfig 方式,看起来更明显。ide
package cn.felord.boot.config;
import cn.felord.boot.event.EatEvent;
import cn.felord.boot.event.EatEventListener;
import cn.felord.boot.event.EatEventPublisherAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * 这三个必定要配置成bean * * @author dax * @since 2019/7/8 22:16 */
@Configuration
public class EventConfig {
@Bean
public ApplicationEvent eatEvent() {
return new EatEvent(true);
}
@Bean
public ApplicationListener eatEventListener() {
return new EatEventListener();
}
@Bean
public ApplicationEventPublisherAware eatEventPublisherAware(ApplicationEvent eatEvent) {
return new EatEventPublisherAware(eatEvent);
}
}
复制代码
这里就大功告成了,那么如何使用呢,执行事件发布器的发布方法 refreshEvent
就好了,事件监听器监听到事件会自动响应。咱们来写一个单元测试。单元测试
package cn.felord.boot;
import cn.felord.boot.event.EatEventPublisherAware;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EventSpringApplicationTests {
@Resource
private EatEventPublisherAware eatEventPublisherAware;
@Test
public void contextLoads() {
eatEventPublisherAware.refreshEvent();
}
}
复制代码
运行一下,入图学习
到此你应该就学会使用 Spring 事件了,这样写出来的代码逼格更高。还能提现你对 Spring 框架的一些理解。固然还有一种更加简单的、基于注解的方式,这里再也不阐述。相关代码在个人 码云仓库测试
关注公众号:Felordcn获取更多资讯
this