暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Spring源码分析之组件扫描事件监听处理器

徘徊笔记 2019-07-19
211


判断是否支持JPA来注册持久化bean处理器

    // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
    if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
    RootBeanDefinition def = new RootBeanDefinition();
    try {
    def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
    AnnotationConfigUtils.class.getClassLoader()));
    }
    catch (ClassNotFoundException ex) {
    throw new IllegalStateException(
    "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
    }
    def.setSource(source);
    beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
    }


    if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
    RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
    def.setSource(source);
    beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
    }
    if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
    RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
    def.setSource(source);
    beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
    }


    注册事件监听处理器类EventListenerMethodProcessor,注册默认事件监听器工厂

      if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
      RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
      def.setSource(source);
      beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
      }
      if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
      RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
      def.setSource(source);
      beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
      }


      它实现了接口SmartInitializingSingleton,在容器初始化最后会触发方法afterSingletonsInstantiated

        public void afterSingletonsInstantiated() {
        List<EventListenerFactory> factories = getEventListenerFactories();
        String[] beanNames = this.applicationContext.getBeanNamesForType(Object.class);
        for (String beanName : beanNames) {
        if (!ScopedProxyUtils.isScopedTarget(beanName)) {
        Class<?> type = null;
        try {
        type = AutoProxyUtils.determineTargetClass(this.applicationContext.getBeanFactory(), 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 {
        type = AutoProxyUtils.determineTargetClass(this.applicationContext.getBeanFactory(),
        ScopedProxyUtils.getTargetBeanName(beanName));
        }
        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(factories, beanName, type);
        }
        catch (Throwable ex) {
        throw new BeanInitializationException("Failed to process @EventListener " +
        "annotation on bean with name '" + beanName + "'", ex);
        }
        }
        }
        }
        }


        获取容器上下文中事件监听器工厂类

          protected List<EventListenerFactory> getEventListenerFactories() {
          Map<String, EventListenerFactory> beans = this.applicationContext.getBeansOfType(EventListenerFactory.class);
          List<EventListenerFactory> allFactories = new ArrayList<EventListenerFactory>(beans.values());
          AnnotationAwareOrderComparator.sort(allFactories);
          return allFactories;
          }


          判断bean名字是否是目标代理范围

            private static final String TARGET_NAME_PREFIX = "scopedTarget.";
            public static boolean isScopedTarget(String beanName) {
            return (beanName != null && beanName.startsWith(TARGET_NAME_PREFIX));
            }


            获取bean类型

              public static final String ORIGINAL_TARGET_CLASS_ATTRIBUTE =
              Conventions.getQualifiedAttributeName(AutoProxyUtils.class, "originalTargetClass");


              public static Class<?> determineTargetClass(ConfigurableListableBeanFactory beanFactory, String beanName) {
              if (beanName == null) {
              return null;
              }
              if (beanFactory.containsBeanDefinition(beanName)) {
              BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
              Class<?> targetClass = (Class<?>) bd.getAttribute(ORIGINAL_TARGET_CLASS_ATTRIBUTE);
              if (targetClass != null) {
              return targetClass;
              }
              }
              return beanFactory.getType(beanName);
              }


              对bean进行处理,检查类型的方法中是否还有注解EventListener

                protected void processBean(final List<EventListenerFactory> factories, final String beanName, final Class<?> targetType) {
                if (!this.nonAnnotatedClasses.contains(targetType)) {
                Map<Method, EventListener> annotatedMethods = null;
                try {
                annotatedMethods = MethodIntrospector.selectMethods(targetType,
                new MethodIntrospector.MetadataLookup<EventListener>() {
                @Override
                public EventListener inspect(Method method) {
                return 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);
                }
                }
                else {
                // Non-empty set of methods
                for (Method method : annotatedMethods.keySet()) {
                for (EventListenerFactory factory : factories) {
                if (factory.supportsMethod(method)) {
                Method methodToUse = AopUtils.selectInvocableMethod(
                method, this.applicationContext.getType(beanName));
                ApplicationListener<?> applicationListener =
                factory.createApplicationListener(beanName, targetType, methodToUse);
                if (applicationListener instanceof ApplicationListenerMethodAdapter) {
                ((ApplicationListenerMethodAdapter) applicationListener)
                .init(this.applicationContext, this.evaluator);
                }
                this.applicationContext.addApplicationListener(applicationListener);
                break;
                }
                }
                }
                if (logger.isDebugEnabled()) {
                logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" +
                beanName + "': " + annotatedMethods);
                }
                }
                }
                }


                查看监听器工厂是否支持该方法,然后创建对应的上下文监听器

                  public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
                  return new ApplicationListenerMethodAdapter(beanName, type, method);
                  }


                  public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
                  this.beanName = beanName;
                  this.method = method;
                  this.targetClass = targetClass;
                  this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);


                  EventListener ann = AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class);
                  this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
                  this.condition = (ann != null ? ann.condition() : null);
                  this.order = resolveOrder(method);


                  this.methodKey = new AnnotatedElementKey(method, targetClass);
                  }


                  初始化它的上下文属性和事件表达式解析类,最后添加到上下文中

                    void init(ApplicationContext applicationContext, EventExpressionEvaluator evaluator) {
                    this.applicationContext = applicationContext;
                    this.evaluator = evaluator;
                    }

                    文章转载自徘徊笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                    评论