介紹
Spring的監(jiān)聽(tīng)器也可以說(shuō)是一種觀察者模式,它能實(shí)現(xiàn)事件與事件監(jiān)聽(tīng)者直接的解耦,在Spring中監(jiān)聽(tīng)器的實(shí)現(xiàn)主要有一下重要組件:
- ApplicationListener:事件監(jiān)聽(tīng)者,觀察者;
- ApplicationEvent:Spring 事件,記錄事件源、事件內(nèi)容、時(shí)間等數(shù)據(jù);
- 有些場(chǎng)景事件主體主要是String或基本類(lèi)型,4.2版本之后,不再?gòu)?qiáng)制要求繼承ApplicationEvent,非ApplicationEvent子類(lèi)的對(duì)象將被包裝成PayloadApplicationEvent
- @EventListener:除了實(shí)現(xiàn)ApplicationListener接口注冊(cè)監(jiān)聽(tīng)器,也可以使用注解的方式
- ApplicationEventPublisher:發(fā)布事件;
事件監(jiān)聽(tīng)4種方式
springboot進(jìn)行事件監(jiān)聽(tīng)有四種方式:
- 手工向ApplicationContext中添加監(jiān)聽(tīng)器
- 使用注解將監(jiān)聽(tīng)器裝載入spring容器
- 在application.properties中配置監(jiān)聽(tīng)器
- 通過(guò)@EventListener注解實(shí)現(xiàn)事件監(jiān)聽(tīng)
講到事件監(jiān)聽(tīng),這里我們說(shuō)下自定義事件和自定義監(jiān)聽(tīng)器類(lèi)的實(shí)現(xiàn)方式:
-
自定義事件:繼承自
ApplicationEvent
抽象類(lèi),然后定義自己的構(gòu)造器 -
自定義監(jiān)聽(tīng):實(shí)現(xiàn)
ApplicationListener<T>
接口,然后實(shí)現(xiàn)onApplicationEvent方法。注意:該接口的實(shí)現(xiàn)類(lèi)必須放到IOC容器中,否者不會(huì)起作用。
自定義事件
import org.springframework.context.ApplicationEvent;
/**
* created at 2023/4/18 10:43
*
* @author somnuszpli
*/
public class MyEvent extends ApplicationEvent {
private Long id;
private String name;
public MyEvent(Long id, String name) {
super(id);
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
發(fā)布事件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
/**
* created at 2023/4/18 10:52
*
* @author somnuszpli
*/
@Component
public class MyEventPublisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publishEvent(Long id, String name) {
MyEvent event = new MyEvent(id,name);
applicationEventPublisher.publishEvent(event);
}
}
1. 手工向ApplicationContext中添加監(jiān)聽(tīng)器
首先創(chuàng)建MyListener1類(lèi)
public class MyListener1 implements ApplicationListener<MyEvent>{
Logger logger = Logger.getLogger(MyListener1.class);
public void onApplicationEvent(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener1.class.getName(), event.getSource()));
}
}
然后在springboot應(yīng)用啟動(dòng)類(lèi)中獲取ConfigurableApplicationContext上下文,裝載監(jiān)聽(tīng)
@SpringBootApplication
public class LisenterApplication{
public static void main(String[] args){
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
//裝載監(jiān)聽(tīng)
context.addApplicationListener(new MyListener1());
}
}
2. 使用注解將監(jiān)聽(tīng)器裝載入spring容器
創(chuàng)建MyListener2類(lèi),并使用@Component注解將該類(lèi)裝載入spring容器中
@Component
public class MyListener2 implements ApplicationListener<MyEvent>{
Logger logger = Logger.getLogger(MyListener2.class);
public void onApplicationEvent(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener2.class.getName(), event.getSource()));
}
}
3. 在application.properties中配置監(jiān)聽(tīng)器
首先創(chuàng)建MyListener3類(lèi)
public class MyListener3 implements ApplicationListener<MyEvent>{
Logger logger = Logger.getLogger(MyListener3.class);
public void onApplicationEvent(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener3.class.getName(), event.getSource()));
}
}
然后在application.properties中配置監(jiān)聽(tīng)
context.listener.classes=com.listener.MyListener3
4. 通過(guò)@EventListener注解實(shí)現(xiàn)事件監(jiān)聽(tīng)
創(chuàng)建MyListener4類(lèi),該類(lèi)無(wú)需實(shí)現(xiàn)ApplicationListener接口,使用@EventListener裝飾具體方法
@Component
public class MyListener4{
Logger logger = Logger.getLogger(MyListener4.class);
// @EventListener 注解支持根據(jù)Event參數(shù)類(lèi)型進(jìn)行匹配
@EventListener
public void listener(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener4.class.getName(), event.getSource()));
}
}
PayloadApplicationEvent
有些時(shí)候我們事件傳遞的對(duì)象是一些簡(jiǎn)單的對(duì)象,比如一個(gè)字符串,不想繼承ApplicationEvent對(duì)象,可以使用PayloadApplicationEvent文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-485096.html
@Component
public class PayloadApplicationListener implements ApplicationListener<PayloadApplicationEvent<String>> {
@Override
public void onApplicationEvent(PayloadApplicationEvent<String> event) {
System.out.println(event.getPayload());
}
}
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
//applicationContext.publishEvent(new InitializePost(applicationContext, "Y"));
applicationContext.publishEvent("hello world");
//applicationContext.publishEvent(new C());
}
運(yùn)行結(jié)果文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-485096.html
hello world
到了這里,關(guān)于SpringBoot 監(jiān)聽(tīng)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!