spring 事件为bean 与 bean之间传递消息。一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件.
spring事件使用步骤如下:
1.先自定义事件:你的事件需要继承 ApplicationEvent
2.定义事件监听器: 需要实现 ApplicationListener
3.使用容器对事件进行发布
以下例子是场景是注册的时候发送邮件的一个场景:
先定义事件:
package com.example.springdemo.event;
import org.springframework.context.ApplicationEvent;
public class EmailEvent extends ApplicationEvent {
private static final long serialVersionUID = -7387041334869267078L;
public EmailEvent(Object source, String msg, String email) {
super(source);
this.msg = msg;
this.email = email;
}
private String msg;
private String email;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
然后定义事件监听器:
package com.example.springdemo.event;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class EmailListener implements ApplicationListener<EmailEvent>{
//使用注解@Async支持 这样不仅可以支持通过调用,也支持异步调用,非常的灵活
@Async
@Override
public void onApplicationEvent(EmailEvent event) {
System.out.println("收到了邮件:" + event.getEmail() + ";内容:" + event.getMsg());
}
}
上述代码也可用@EventListener替换,无需实现类,可随便定义方法名,如:
@Component public class EmailListener{ @EventListener public void receive(EmailEvent event) { System.out.println("收到了邮件:" + event.getEmail() + ";内容:" + event.getMsg()); } }
再次使用容器对事件进行发布:
package com.example.springdemo.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class EmailPublisher {
@Autowired
private ApplicationContext applicationContext;
public void publish(String email, String msg) {
applicationContext.publishEvent(new EmailEvent(this, msg, email));
}
}
最后就是测试了:
package com.example.springdemo.event;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.springdemo.event")
public class EmailConfig {
}
package com.example.springdemo.event;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringEventTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(EmailConfig.class);
EmailPublisher publisher = context.getBean(EmailPublisher.class);
publisher.publish("张三", "雷猴啊");
context.close();
}
}
结果:
19:35:42.535 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'emailPublisher' 19:35:42.536 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'emailListener' 收到了邮件:张三;内容:雷猴啊 19:35:42.537 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ecddf8f: startup date [Mon Jan 21 19:35:42 CST 2019]; root of context hierarchy 19:35:42.537 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor' 19:35:42.537 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@77cd7a0: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,emailConfig,emailListener,emailPublisher]; root of factory hierarchy
