找回密码
 立即注册
首页 业界区 业界 Spring BeanPostProcessor接口

Spring BeanPostProcessor接口

尸酒岐 2025-11-13 21:35:02
[[Spring IOC 源码学习总笔记]]
BeanPostProcessor

BeanPostProcessor是 Spring 框架提供的一个扩展点接口,它允许开发者在 Spring 容器完成 Bean 的实例化、依赖注入之后,在初始化阶段的前后“拦截”并自定义 Bean 的逻辑。
  1. package org.springframework.beans.factory.config;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.lang.Nullable;
  4. public interface BeanPostProcessor {
  5.         /**
  6.          * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
  7.          * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
  8.          * or a custom init-method). The bean will already be populated with property values.
  9.          * The returned bean instance may be a wrapper around the original.
  10.          * <p>The default implementation returns the given {@code bean} as-is.
  11.          *
  12.          *这个是在 Bean 实例化并且填充属性之后调用, 但是 Bean 中一些生命周期方法如 InitializingBean 接口的
  13.          * afterPropertiesSet 方法、自定义的 init-method 方法等都尚未执行,在这些方法执行之前触发 postProcessBeforeInitialization 方法。
  14.          *
  15.          * @param bean the new bean instance
  16.          * @param beanName the name of the bean
  17.          * @return the bean instance to use, either the original or a wrapped one;
  18.          * if {@code null}, no subsequent BeanPostProcessors will be invoked
  19.          * @throws org.springframework.beans.BeansException in case of errors
  20.          * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
  21.          */
  22.         @Nullable
  23.         default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  24.                 return bean;
  25.         }
  26.         /**
  27.          * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
  28.          * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
  29.          * or a custom init-method). The bean will already be populated with property values.
  30.          * The returned bean instance may be a wrapper around the original.
  31.          * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
  32.          * instance and the objects created by the FactoryBean (as of Spring 2.0). The
  33.          * post-processor can decide whether to apply to either the FactoryBean or created
  34.          * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
  35.          * <p>This callback will also be invoked after a short-circuiting triggered by a
  36.          * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
  37.          * in contrast to all other {@code BeanPostProcessor} callbacks.
  38.          * <p>The default implementation returns the given {@code bean} as-is.
  39.          *
  40.          *  在 InitializingBean 接口的 afterPropertiesSet 和自定义的 init-method 之后触发该方法。
  41.          *
  42.          * @param bean the new bean instance
  43.          * @param beanName the name of the bean
  44.          * @return the bean instance to use, either the original or a wrapped one;
  45.          * if {@code null}, no subsequent BeanPostProcessors will be invoked
  46.          * @throws org.springframework.beans.BeansException in case of errors
  47.          * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
  48.          * @see org.springframework.beans.factory.FactoryBean
  49.          */
  50.         @Nullable
  51.         default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  52.                 return bean;
  53.         }
  54. }
复制代码
接口中的两个方法都要将传入的 bean 返回,而不能返回 null,如果返回的是 null 那么我们通过 getBean() 方法将得不到目标对象。
可见:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization 的源码调用逻辑
  1. @Deprecated(since = "6.1")
  2. @Override
  3. public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
  4.                 throws BeansException {
  5.         Object result = existingBean;
  6.         for (BeanPostProcessor processor : getBeanPostProcessors()) {
  7.                 Object current = processor.postProcessBeforeInitialization(result, beanName);
  8.                 if (current == null) {
  9.                         return result;
  10.                 }
  11.                 result = current;
  12.         }
  13.         return result;
  14. }
复制代码
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization
  1. @Deprecated(since = "6.1")
  2. @Override
  3. public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
  4.                 throws BeansException {
  5.         Object result = existingBean;
  6.         for (BeanPostProcessor processor : getBeanPostProcessors()) {
  7.                 Object current = processor.postProcessAfterInitialization(result, beanName);
  8.                 if (current == null) {
  9.                         return result;
  10.                 }
  11.                 result = current;
  12.         }
  13.         return result;
  14. }
复制代码
BeanPostProcessor 的子接口

另外 BeanPostProcessor 粗粒度太大, Spring 还细分一些子接口:
1.png

AutowiredAnnotationBeanPostProcessor

@Autowired、@Inject  等就是根据这个回调来实现最终注入依赖的属性的。
  1. public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
  2.         // BeanPostProcessor 接口方法
  3.         @Nullable
  4.         default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
  5.                 return null;
  6.         }
  7.         // BeanPostProcessor 接口方法
  8.         default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
  9.                 return true;
  10.         }
  11.         /**
  12.          * Post-process the given property values before the factory applies them
  13.          * to the given bean.
  14.          * <p>The default implementation returns the given {@code pvs} as-is.
  15.          *
  16.          * 将给定的属性值应用到指定的bean之前进行回调
  17.          * 可以用来检查和修改属性,最终返回的PropertyValues会应用到bean中
  18.          *         `@Autowired、@Resource` 在Spring中 就是根据这个回调来实现最终注入依赖的属性的
  19.          *        
  20.          * @param pvs the property values that the factory is about to apply (never {@code null})
  21.          * @param bean the bean instance created, but whose properties have not yet been set
  22.          * @param beanName the name of the bean
  23.          * @return the actual property values to apply to the given bean (can be the passed-in
  24.          * PropertyValues instance), or {@code null} to skip property population
  25.          * @throws org.springframework.beans.BeansException in case of errors
  26.          * @since 5.1
  27.          */
  28.         @Nullable
  29.         default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
  30.                         throws BeansException {
  31.                 return pvs;
  32.         }
  33. }
复制代码
postProcessProperties 在哪里调用呢?
相关源码

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 填充Bean属性方法
  1. /**
  2.          * Populate the bean instance in the given BeanWrapper with the property values
  3.          * from the bean definition.
  4.          * @param beanName the name of the bean
  5.          * @param mbd the bean definition for the bean
  6.          * @param bw the BeanWrapper with bean instance
  7.          */
  8.         protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
  9.                 // 验证一下入参
  10.                 if (bw == null) {
  11.                         if (mbd.hasPropertyValues()) {
  12.                                 throw new BeanCreationException(
  13.                                                 mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
  14.                         }
  15.                         else {
  16.                                 // Skip property population phase for null instance.
  17.                                 return;
  18.                         }
  19.                 }
  20.                 if (bw.getWrappedClass().isRecord()) {
  21.                         if (mbd.hasPropertyValues()) {
  22.                                 throw new BeanCreationException(
  23.                                                 mbd.getResourceDescription(), beanName, "Cannot apply property values to a record");
  24.                         }
  25.                         else {
  26.                                 // Skip property population phase for records since they are immutable.
  27.                                 return;
  28.                         }
  29.                 }
  30.                 // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
  31.                 // state of the bean before properties are set. This can be used, for example,
  32.                 // to support styles of field injection.
  33.                 /**
  34.                  * 如果是AOP, pointcut, advice相关的, synthetic 会配置为 true
  35.                  */
  36.                 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
  37.                         for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
  38.                                 if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
  39.                                         return;
  40.                                 }
  41.                         }
  42.                 }
  43.                 /**
  44.                  * 包含一个或多个{@link PropertyValue}对象的Holder,通常针对特定目标bean的一次更新
  45.                  * 可以理解为: 该bean所有属性的描述
  46.                  */
  47.                 PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
  48.                 /**
  49.                  * 获取自动注入的方式
  50.                  */
  51.                 int resolvedAutowireMode = mbd.getResolvedAutowireMode();
  52.                 if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
  53.                         /**
  54.                          * 该分支处理通过 名称或者类型 注入的属性
  55.                          * (有注解才会走这个分支)
  56.                          * ===============================
  57.                          */
  58.                         MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
  59.                         // Add property values based on autowire by name if applicable.
  60.                         if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
  61.                                 /**
  62.                                  * 通过名称注入
  63.                                  */
  64.                                 autowireByName(beanName, mbd, bw, newPvs);
  65.                         }
  66.                         // Add property values based on autowire by type if applicable.
  67.                         if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
  68.                                 /**
  69.                                  * 通过类型注入
  70.                                  */
  71.                                 autowireByType(beanName, mbd, bw, newPvs);
  72.                         }
  73.                         pvs = newPvs;
  74.                 }
  75.                 /**
  76.                  *  <!> 回调所有 InstantiationAwareBeanPostProcessor#postProcessProperties 方法
  77.                  *  比如, `@Autowired` 的处理对应实现类: {@link org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor}
  78.                  *  (作用是: 在工厂将属性值应用到给定bean之前,对它们进行处理)
  79.                  *
  80.                  */
  81.                 if (hasInstantiationAwareBeanPostProcessors()) {
  82.                         if (pvs == null) {
  83.                                 pvs = mbd.getPropertyValues();
  84.                         }
  85.                         // <!> 回调InstantiationAwareBeanPostProcessor#postProcessProperties 方法
  86.                         for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
  87.                                 PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
  88.                                 if (pvsToUse == null) {
  89.                                         return;
  90.                                 }
  91.                                 pvs = pvsToUse;
  92.                         }
  93.                 }
  94. ....
复制代码
回调InstantiationAwareBeanPostProcessor#postProcessProperties 方法
后面注入在: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties
  1. @Override
  2. public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
  3.         InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
  4.         try {
  5.                 // 注入
  6.                 metadata.inject(bean, beanName, pvs);
  7.         }
  8.         catch (BeanCreationException ex) {
  9.                 throw ex;
  10.         }
  11.         catch (Throwable ex) {
  12.                 throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
  13.         }
  14.         return pvs;
  15. }
复制代码
InstantiationAwareBeanPostProcessor & SmartInstantiationAwareBeanPostProcessor

SmartInstantiationAwareBeanPostProcessor 它提供了更高级的Bean实例化控制方法。主要作用在于允许对Bean的实例化过程进行更精细的控制和定制。
  1. public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
  2.     //用来返回目标对象的类型(比如代理对象通过raw class获取proxy type 用于类型匹配)
  3.     @Nullable
  4.     default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
  5.         return null;
  6.     }
  7.     //这里提供一个拓展点用来解析获取用来实例化的构造器(比如未通过bean定义构造器以及参数的情况下,会根据这个回调来确定构造器)
  8.     @Nullable
  9.     default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
  10.             throws BeansException {
  11.         return null;
  12.     }
  13.     //获取要提前暴露的bean的引用,用来支持单例对象的循环引用(一般是bean自身,如果是代理对象则需要取用代理引用)
  14.     default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
  15.         return bean;
  16.     }
  17. }
复制代码
相关源码

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
  1.         @Override
  2.         protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
  3.                         throws BeanCreationException {
  4.                 if (logger.isTraceEnabled()) {
  5.                         logger.trace("Creating instance of bean '" + beanName + "'");
  6.                 }
  7.                 RootBeanDefinition mbdToUse = mbd;
  8.                 /**
  9.                  * 1. 解析到 BeanDefinition 的 Class
  10.                  */
  11.                 Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
  12.                 /**
  13.                  * 2. 创建BeanDefinition 复制一份 RootBeanDefinition
  14.                  */
  15.                 if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
  16.                         mbdToUse = new RootBeanDefinition(mbd);
  17.                         mbdToUse.setBeanClass(resolvedClass);
  18.                 }
  19.                 // Prepare method overrides.
  20.                 try {
  21.                         /**
  22.                          * 3. 准备和验证 lookup-method 和 replace-method;
  23.                          * 注意: 这里只是标记, 真正处理是在实例化时, 选择策略生成一个CgLib的代理对象 {@link org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy}
  24.                          * 关于它们的作用见笔记 [[Spring lookup-method 和 replace-method.md]]
  25.                          */
  26.                         mbdToUse.prepareMethodOverrides();
  27.                 }
  28.                 catch (BeanDefinitionValidationException ex) {
  29.                         throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
  30.                                         beanName, "Validation of method overrides failed", ex);
  31.                 }
  32.                 try {
  33.                         /**
  34.                          * 给 BeanPostProcessors 一个返回代理实例的机会;
  35.                          * 注意, 这里的逻辑:
  36.                          * 如果其 BeanPostProcessors的子接口返回不为null, 则直接使用这个bean实例返回了, 不走 doCreateBean流程了
  37.                          * (注意的注意! 如果有用户自定义的拦截创建, 甚至优先Spring的AOP代理创建)
  38.                          */
  39.                         Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
  40.                         if (bean != null) {
  41.                                 return bean;
  42.                         }
  43.                 }
  44.                 catch (Throwable ex) {
  45.                         throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
  46.                                         "BeanPostProcessor before instantiation of bean failed", ex);
  47.                 }
复制代码
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation
  1. protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
  2.         Object bean = null;
  3.         if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
  4.                 // Make sure bean class is actually resolved at this point.
  5.                 // 如果是合成的(mbd是AOP的时候,为true)并且实现 InstantiationAwareBeanPostProcessor 接口
  6.                 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
  7.                         Class<?> targetType = determineTargetType(beanName, mbd);
  8.                         if (targetType != null) {
  9.                                 // 使用 InstantiationAwareBeanPostProcessor接口 生成的 bean 返回
  10.                                 bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
  11.                                 if (bean != null) {
  12.                                         bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
  13.                                 }
  14.                         }
  15.                 }
  16.                 mbd.beforeInstantiationResolved = (bean != null);
  17.         }
  18.         return bean;
  19. }
复制代码
DestructionAwareBeanPostProcessor

DestructionAwareBeanPostProcessor 它允许在Bean被销毁之前(例如,容器关闭或特定作用域的Bean销毁)执行一些操作。
org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
  1. public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
  2.    
  3.     //这里实现销毁对象的逻辑
  4.     void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
  5.    
  6.     //判断是否需要处理这个对象的销毁
  7.     default boolean requiresDestruction(Object bean) {
  8.         return true;
  9.     }
  10. }
复制代码
MergedBeanDefinitionPostProcessor

MergedBeanDefinitionPostProcessor 算是整个 BeanPostProcessor 家族中比较另类的一个接口了,它虽然是 BeanPostProcessor,但是却可以处理 BeanDefinition。
MergedBeanDefinitionPostProcessor 介入的时机就是 Bean 创建成功之后,Bean 中各个属性填充之前。
相关源码

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
  1. protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
  2.                         throws BeanCreationException {
  3.                 // Instantiate the bean.
  4.                 BeanWrapper instanceWrapper = null;
  5.                 if (mbd.isSingleton()) {
  6.                         instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  7.                 }
  8.                 if (instanceWrapper == null) {
  9.                         /**
  10.                          *  1. 真正在jvm层面实例化对象;
  11.                          *    1.1 如果该 BeanDefinition 有配置 instanceSupplier属性(java.util.function.Supplier),
  12.                          *                  将使用其Supplier#get方法的返回, 作为实例对象
  13.                          *    1.2 如果该 BeanDefinition 有配置 factory-method 将使用该方法返回的, 作为实例对象
  14.                          *                  这里这里包括'静态工厂'和'实例工厂'的处理(通过bd的factoryBeanName 进行判断, 如果存在则是'实例工厂')
  15.                          *           1.3 使用反射实例化, 根据策略实例化Bean对象 {@link org.springframework.beans.factory.support.SimpleInstantiationStrategy}
  16.                          *            - 使用有参构造函数注入,创建 (new)
  17.                          *            - 使用无参构造函数创建 (new)
  18.                          *            - 工厂方法实例化 ('静态工厂' '实例工厂' )
  19.                          *              `<bean  factory-bean="bookFactoryBean"  factory-method="getBook()"/>`
  20.                          *                 静态工厂: factory-bean 若是 全限定类名 则使用 BookFactoryBean::getBook 的静态方法返回的对象
  21.                          *                 实例工厂:  factory-bean 若是 bean 名 则使用该实例方法返回的对象
  22.                          *         {@link AbstractAutowireCapableBeanFactory#createBeanInstance(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])}
  23.                          *  2. 用BeanWrapper 包装原始bean (装饰模式)
  24.                          * {@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#instantiateBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition)}
  25.                          *
  26.                          */
  27.                         instanceWrapper = createBeanInstance(beanName, mbd, args);
  28.                 }
  29.                 /**
  30.                  * 获取 BeanWrapper中的原始 Bean 实例
  31.                  */
  32.                 Object bean = instanceWrapper.getWrappedInstance();
  33.                 /**
  34.                  * 获取Bean Class类型
  35.                  */
  36.                 Class<?> beanType = instanceWrapper.getWrappedClass();
  37.                 if (beanType != NullBean.class) {
  38.                         mbd.resolvedTargetType = beanType;
  39.                 }
  40.                 // Allow post-processors to modify the merged bean definition.
  41.                 synchronized (mbd.postProcessingLock) {
  42.                         if (!mbd.postProcessed) {
  43.                                 try {
  44.                                         /**
  45.                                          * 实例化完了, 处理 MergedBeanDefinitionPostProcessor 的接口回调
  46.                                          */
  47.                                         applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
  48.                                 }
  49.                                 catch (Throwable ex) {
  50.                                         throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  51.                                                         "Post-processing of merged bean definition failed", ex);
  52.                                 }
  53.                                 mbd.markAsPostProcessed();
  54.                         }
  55.                 }
  56.                 // Eagerly cache singletons to be able to resolve circular references
  57.                 // even when triggered by lifecycle interfaces like BeanFactoryAware.
  58.                 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
  59.                                 isSingletonCurrentlyInCreation(beanName));
  60.                 if (earlySingletonExposure) {
  61.                         if (logger.isTraceEnabled()) {
  62.                                 logger.trace("Eagerly caching bean '" + beanName +
  63.                                                 "' to allow for resolving potential circular references");
  64.                         }
  65.                         addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
  66.                 }
  67.                 // Initialize the bean instance.
  68.                 Object exposedObject = bean;
  69.                 try {
  70.                         /**
  71.                          * 填充属性
  72.                          */
  73.                         populateBean(beanName, mbd, instanceWrapper);
  74.                         exposedObject = initializeBean(beanName, exposedObject, mbd);
  75.                 }
  76. .....
复制代码
处理 init-method, @PostConstruct, @PreDestroy

对应子类 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
注意: 这里只是查询到封装为元信息保存到 BeanDefinition, 还不会调用
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor#findLifecycleMetadata(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Class)
  1. private LifecycleMetadata findLifecycleMetadata(RootBeanDefinition beanDefinition, Class<?> beanClass) {  
  2. /**
  3. 查找相应的注解方法信息, 封装为 LifecycleMetadata 元数据(使用集合存起来包含: initMethods 列表,destroyMethods列表...)
  4. */
  5.    LifecycleMetadata metadata = findLifecycleMetadata(beanClass);
  6. /**
  7. 再将这些 LifecycleMetadata 元数据, 注册(修改)到 BeanDefinition 中
  8. beanDefinition.registerExternallyManagedInitMethod(methodIdentifier);
  9. beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier);
  10. **/
  11.    metadata.checkInitDestroyMethods(beanDefinition);  
  12.    return metadata;  
  13. }
复制代码
处理 @Autowired

对应子类 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
注意: 这里只是查询到封装为元信息保存到 BeanDefinition, 还不会调用
雷同的逻辑
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#findInjectionMetadata
  1.         private InjectionMetadata findInjectionMetadata(String beanName, Class<?> beanType, RootBeanDefinition beanDefinition) {
  2.                 // 查找  注解方法信息, 封装为元数据对象
  3.                 InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);
  4.                 // 注册(修改)到 BeanDefinition 中
  5.                 metadata.checkConfigMembers(beanDefinition);
  6.                 return metadata;
  7.         }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

2025-11-19 23:46:31

举报

喜欢鼓捣这些软件,现在用得少,谢谢分享!
您需要登录后才可以回帖 登录 | 立即注册