监听容器是如何初始化的?
当程序执行到AbstractAutowireCapableBeanFactory(BeanPostProcessor的实现类,保证后续bean可能被包装)initializeBean()方法时,轮询执行postProcessAfterInitialization ,该BeanPostProcessor是在容器执行registerBeanPostProcessors时候内置加载到bean工厂的
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
ApplicationListenerDetector.postProcessAfterInitialization 向容器ApplicationContext加入事件监听器
public void addApplicationListener(ApplicationListener<?> listener) {
Assert.notNull(listener, "ApplicationListener must not be null");
if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener);
}
this.applicationListeners.add(listener);
}
当然并不是所有类都会在initializeBean()方法时,执行postProcessAfterInitialization注入到监听容器中,因为执行时会进行类型判断,如下代码 bean instanceof ApplicationListener
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof ApplicationListener) {
// potentially not detected as a listener by getBeanNamesForType retrieval
Boolean flag = this.singletonNames.get(beanName);
if (Boolean.TRUE.equals(flag)) {
// singleton bean (top-level or inner): register on the fly
this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
}
else if (Boolean.FALSE.equals(flag)) {
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
// inner bean with other scope - can't reliably process events
logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
"but is not reachable for event multicasting by its containing ApplicationContext " +
"because it does not have singleton scope. Only top-level listener beans are allowed " +
"to be of non-singleton scope.");
}
this.singletonNames.remove(beanName);
}
}
return bean;
}
如何创建监听器?
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class ProcessListener implements ApplicationListener<Myevent> {
@Async
@Override
public void onApplicationEvent(Myevent event) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("监听到了:"+event.getSource()+event.name+"--"+event.age);
}
}
或者
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class AnnListener {
@EventListener
public void say(Myevent event){
System.out.println("注解监听到了:"+event.getSource()+event.name+"--"+event.age);
}
}
如何执行监听事件?
可以通过ApplicationContext容器发布事件触发
@Autowired
private ApplicationContext context;
public void getEvent(){
context.publishEvent(new Myevent());
}
循环监听器,通过事件类型获取,也就是说通过下面代码重的getApplicationListeners进行获取,条件(this.declaredEventType == null || this.declaredEventType.isAssignableFrom(eventType)),事件条件具有继承关系
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
20200707更新
对于实现ApplicationListener<E extends ApplicationEvent>接口,重写void onApplicationEvent(E event);方法来说,监听器并没有存储event到具体的成员变量或容器里边,那么当applicationContext.publishEvent发布事件的时候,如何根据事件类型找到对应的监听器呢?
答案是对于监听器的class文件被包装成ResolvableType类型,获取ResolvableType类型参数,与发布事件类型做比对,获取方法如下
public ResolvableType[] getGenerics() {
if (this == NONE) {
return EMPTY_TYPES_ARRAY;
}
ResolvableType[] generics = this.generics;
if (generics == null) {
if (this.type instanceof Class) {
Type[] typeParams = ((Class<?>) this.type).getTypeParameters();
generics = new ResolvableType[typeParams.length];
for (int i = 0; i < generics.length; i++) {
generics[i] = ResolvableType.forType(typeParams[i], this);
}
}
else if (this.type instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) this.type).getActualTypeArguments();
generics = new ResolvableType[actualTypeArguments.length];
for (int i = 0; i < actualTypeArguments.length; i++) {
generics[i] = forType(actualTypeArguments[i], this.variableResolver);
}
}
else {
generics = resolveType().getGenerics();
}
this.generics = generics;
}
return generics;
}
之后通过GenericApplicationListenerAdapter监听适配器进行类别比对:
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClass());
if (declaredEventType == null || declaredEventType.isAssignableFrom(ApplicationEvent.class)) {
Class<?> targetClass = AopUtils.getTargetClass(listener);
if (targetClass != listener.getClass()) {
declaredEventType = resolveDeclaredEventType(targetClass);
}
}
return declaredEventType;
}