前面有笔记,记录过SmartInitializingSingleton这个扩展点的使用,可以参考这篇博客 spring扩展点四:SmartInitializingSingleton的应用
这篇博客是我自己写的一个demo,要验证这个扩展机制,但是最近在看spring事件监听机制的时候,看到了对这个扩展点的应用
EventListenerMethodProcessor ,就是这个bean,我们先不说这个bean是什么注入到spring容器中的,会在后面事件监听器源码的博客中,单独记录,这篇博客主要想说明的是:在spring事件监听机制中,如果我通过@EventListener来声明一个事件监听器,那他就是通过SmartInitializingSingleton这个扩展机制来完成解析的
org.springframework.context.event.EventListenerMethodProcessor#afterSingletonsInstantiated
/**
* 这个方法也是spring扩展点之一
*/
@Override
public void afterSingletonsInstantiated() {
/**
* 1.获取到所有的eventListenerFactory,理论上就是DefaultEventListenerFactory
*/
List<EventListenerFactory> factories = getEventListenerFactories();
ConfigurableApplicationContext context = getApplicationContext();
/**
* 2.获取到spring容器中,所有的beanName,依次遍历
*/
String[] beanNames = context.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
if (!ScopedProxyUtils.isScopedTarget(beanName)) {
Class<?> type = null;
try {
type = AutoProxyUtils.determineTargetClass(context.getBeanFactory(), beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.