一、原理及源码解析
事件:contextrefreshedevent、ioctest_ext$1[source=我发布的事件]、contextclosedevent;
* 1)、contextrefreshedevent事件:
* 1)、容器创建对象:refresh();
* 2)、finishrefresh();容器刷新完成会发布contextrefreshedevent事件
* 2)、自己发布事件;
* 3)、容器关闭会发布contextclosedevent;
*
* 【事件发布流程】源码执行流程:
* 3)、publishevent(new contextrefreshedevent(this));
* 1)、获取事件的多播器(派发器):getapplicationeventmulticaster()
* 2)、multicastevent派发事件:
* 3)、获取到所有的applicationlistener;
* for (final applicationlistener<?> listener : getapplicationlisteners(event, type)) {
* 1)、如果有executor,可以支持使用executor进行异步派发;
* executor executor = gettaskexecutor();
* 2)、否则,同步的方式直接执行listener方法;invokelistener(listener, event);
* 拿到listener回调onapplicationevent方法;
*
* 【事件多播器(派发器)】源码执行流程:
* 1)、容器创建对象:refresh();
* 2)、initapplicationeventmulticaster();初始化applicationeventmulticaster;
* 1)、先去容器中找有没有id=“applicationeventmulticaster”的组件;
* 2)、如果没有this.applicationeventmulticaster = new simpleapplicationeventmulticaster(beanfactory);
* 并且加入到容器中,我们就可以在其他组件要派发事件,自动注入这个applicationeventmulticaster;
*
* 【容器中有哪些监听器】源码执行流程:
* 1)、容器创建对象:refresh();
* 2)、registerlisteners();
* 从容器中拿到所有的监听器,把他们注册到applicationeventmulticaster中;
* string[] listenerbeannames = getbeannamesfortype(applicationlistener.class, true, false);
* //将listener注册到applicationeventmulticaster中
* getapplicationeventmulticaster().addapplicationlistenerbean(listenerbeanname);
*
* smartinitializingsingleton 原理:->aftersingletonsinstantiated();
* 1)、ioc容器创建对象并refresh();
* 2)、finishbeanfactoryinitialization(beanfactory);初始化剩下的单实例bean;
* 1)、先创建所有的单实例bean;getbean();
* 2)、获取所有创建好的单实例bean,判断是否是smartinitializingsingleton类型的;
* 如果是就调用aftersingletonsinstantiated();
*
二、实例
applicationlistener:监听容器中发布的事件。事件驱动模型开发;
* public interface applicationlistener<e extends applicationevent>
* 监听 applicationevent 及其下面的子事件;
*
* 步骤:
* 1)、写一个监听器(applicationlistener实现类)来监听某个事件(applicationevent及其子类)
* @eventlistener;
* 原理:使用eventlistenermethodprocessor处理器来解析方法上的@eventlistener;
*
* 2)、把监听器加入到容器;
* 3)、只要容器中有相关事件的发布,我们就能监听到这个事件;
* contextrefreshedevent:容器刷新完成(所有bean都完全创建)会发布这个事件;
* contextclosedevent:关闭容器会发布这个事件;
* 4)、发布一个事件:
* applicationcontext.publishevent();
1、自定义监听器
定义myapplicationlistener 类并实现applicationlistener<applicationevent>接口
用@component装配到spring容器中
package com.atguigu.ext;
import org.springframework.context.applicationevent;
import org.springframework.context.applicationlistener;
import org.springframework.context.event.eventlistener;
import org.springframework.stereotype.component;
@component
public class myapplicationlistener implements applicationlistener<applicationevent> {
//当容器中发布此事件以后,方法触发
@override
public void onapplicationevent(applicationevent event) {
// todo auto-generated method stub
system.out.println("收到事件:"+event);
}
}或者使用@eventlistener注解来定义一个监听器
注:@eventlistener(classes={applicationevent.class})中classes监听的类可以是多个,以逗号分开
package com.atguigu.ext;
import org.springframework.context.applicationevent;
import org.springframework.context.event.eventlistener;
import org.springframework.stereotype.service;
@service
public class userservice {
@eventlistener(classes={applicationevent.class})
public void listen(applicationevent event){
system.out.println("userservice。。监听到的事件:"+event);
}
}2、定义配置类,加载ioc容器
@componentscan("com.atguigu.ext")
@configuration
public class extconfig {
@bean
public blue blue(){
return new blue();
}
}3、测试
自定义发布事件使用了匿名内部类
package com.atguigu.test;
import org.junit.test;
import org.springframework.context.applicationevent;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import com.atguigu.ext.extconfig;
public class ioctest_ext {
@test
public void test01(){
annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(extconfig.class);
//发布事件;
applicationcontext.publishevent(new applicationevent(new string("我发布的事件")) {
});
applicationcontext.close();
}
}结果:


说明:spring自己有自定义监听器,如容器有刷新和关闭事件发布就会被监听到;自己定义了监听器并装配到spring容器中,一旦有相关事件发布也可以被监听到。
到此这篇关于spring监听器之applicationlistener原理及源码深度解析的文章就介绍到这了,更多相关spring applicationlistener原理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论