1. 程式人生 > 實用技巧 >BeanFactory後置處理器 - EventListenerMethodProcessor

BeanFactory後置處理器 - EventListenerMethodProcessor

EventListenerMethodProcessor 是 BeanFactory 的一個後置處理器, 用來對 @EventListener 提供支援.

主要是標註了 @EventListener 的方法進行解析, 然後轉換為一個ApplicationListener.

1. 在 refresh 容器的時候, 呼叫invokeBeanFactoryPostProcessors() 方法時, 會執行BeanFactoryPostProcessor#postProcessBeanFactory() 方法.

  此方法中, 他只是設定了一個預設的監聽器工廠 :DefaultEventListenerFactory

2. 他實現了SmartInitializingSingleton 介面, 會在

  org.springframework.context.support.AbstractApplicationContext#refresh

  \|/

  org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization

  \|/

  org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons 方法中呼叫其 afterSingletonsInstantiated() 方法.

  其呼叫時機是, 遍歷容器中註冊的 BeanDefinition, 呼叫所有 getBean() 方法之後, 才會開始遍歷執行afterSingletonsInstantiated() 方法.

  這裡最終會呼叫processBean() 方法.

  此方法中, 就會去容器中查詢標註了@EventListener 註解的方法, 然後進行轉換.

/**
 * 這個後置處理器, 主要是處理 @EventListener 註解的.
 * 1. 解析 @EventListener , 獲取攔截方法
 * 2. 對攔截方法進行轉換, 變成 ApplicationListener
 * 3. 將轉換的 ApplicationListener, 放到spring容器中
 * Registers {
@link EventListener} methods as individual {@link ApplicationListener} instances. * Implements {@link BeanFactoryPostProcessor} (as of 5.1) primarily for early retrieval, * avoiding AOP checks for this processor bean and its {@link EventListenerFactory} delegates. * * @author Stephane Nicoll * @author Juergen Hoeller * @since 4.2 * @see EventListenerFactory * @see DefaultEventListenerFactory */ public class EventListenerMethodProcessor implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor { protected final Log logger = LogFactory.getLog(getClass()); @Nullable private ConfigurableApplicationContext applicationContext; @Nullable private ConfigurableListableBeanFactory beanFactory; @Nullable private List<EventListenerFactory> eventListenerFactories; private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator(); private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64)); @Override public void setApplicationContext(ApplicationContext applicationContext) { Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext, "ApplicationContext does not implement ConfigurableApplicationContext"); this.applicationContext = (ConfigurableApplicationContext) applicationContext; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { this.beanFactory = beanFactory; Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false); List<EventListenerFactory> factories = new ArrayList<>(beans.values()); AnnotationAwareOrderComparator.sort(factories); //DefaultEventListenerFactory this.eventListenerFactories = factories; } @Override public void afterSingletonsInstantiated() { ConfigurableListableBeanFactory beanFactory = this.beanFactory; Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set"); String[] beanNames = beanFactory.getBeanNamesForType(Object.class); for (String beanName : beanNames) { if (!ScopedProxyUtils.isScopedTarget(beanName)) { Class<?> type = null; try { type = AutoProxyUtils.determineTargetClass(beanFactory, beanName); } catch (Throwable ex) { // An unresolvable bean type, probably from a lazy bean - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex); } } if (type != null) { if (ScopedObject.class.isAssignableFrom(type)) { try { Class<?> targetClass = AutoProxyUtils.determineTargetClass( beanFactory, ScopedProxyUtils.getTargetBeanName(beanName)); if (targetClass != null) { type = targetClass; } } catch (Throwable ex) { // An invalid scoped proxy arrangement - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex); } } } try { processBean(beanName, type); } catch (Throwable ex) { throw new BeanInitializationException("Failed to process @EventListener " + "annotation on bean with name '" + beanName + "'", ex); } } } } } private void processBean(final String beanName, final Class<?> targetType) { if (!this.nonAnnotatedClasses.contains(targetType) && !targetType.getName().startsWith("java") && !isSpringContainerClass(targetType)) { Map<Method, EventListener> annotatedMethods = null; try { //獲取標註了 @EventListener 註解的監聽方法 annotatedMethods = MethodIntrospector.selectMethods(targetType, (MethodIntrospector.MetadataLookup<EventListener>) method -> AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class)); } catch (Throwable ex) { // An unresolvable type in a method signature, probably from a lazy bean - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex); } } if (CollectionUtils.isEmpty(annotatedMethods)) { this.nonAnnotatedClasses.add(targetType); if (logger.isTraceEnabled()) { logger.trace("No @EventListener annotations found on bean class: " + targetType.getName()); } } else { // Non-empty set of methods ConfigurableApplicationContext context = this.applicationContext; Assert.state(context != null, "No ApplicationContext set"); List<EventListenerFactory> factories = this.eventListenerFactories; Assert.state(factories != null, "EventListenerFactory List not initialized"); for (Method method : annotatedMethods.keySet()) { for (EventListenerFactory factory : factories) { if (factory.supportsMethod(method)) { Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName)); //為監聽方法建立 ApplicationListener ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse); if (applicationListener instanceof ApplicationListenerMethodAdapter) { ((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator); } //將建立的 ApplicationListener 加入到容器中 context.addApplicationListener(applicationListener); break; } } } if (logger.isDebugEnabled()) { logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods); } } } } /** * Determine whether the given class is an {@code org.springframework} * bean class that is not annotated as a user or test {@link Component}... * which indicates that there is no {@link EventListener} to be found there. * @since 5.1 */ private static boolean isSpringContainerClass(Class<?> clazz) { return (clazz.getName().startsWith("org.springframework.") && !AnnotatedElementUtils.isAnnotated(ClassUtils.getUserClass(clazz), Component.class)); } }