• spring bean生命周期源码分析


    idea代码如下:
    1. public static void main(String[] args) throws ExecutionException, InterruptedException {
    2. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    3. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    4. context.registerShutdownHook();
    5. }
    1. package net.biancheng.c;
    2. import org.springframework.beans.BeansException;
    3. import org.springframework.beans.factory.config.BeanPostProcessor;
    4. import org.springframework.core.Ordered;
    5. /**
    6. * @author
    7. * @Description getOrder值越小,越先执行 ,跟xml配置先后顺序没有关系
    8. * @Date 2022/6/10 17:44
    9. */
    10. public class InitHelloWorld implements BeanPostProcessor, Ordered {
    11. private String message ;
    12. public void setMessage(String message) {
    13. System.out.println("message = " + message);
    14. this.message = message;
    15. }
    16. public InitHelloWorld() {
    17. System.out.println("InitHelloWorld()");
    18. }
    19. @Override
    20. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    21. System.out.println("InitHelloWorld Before : " + beanName);
    22. return bean;
    23. }
    24. @Override
    25. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    26. System.out.println("InitHelloWorld After : " + beanName);
    27. return bean;
    28. }
    29. @Override
    30. public int getOrder(){
    31. return 5;
    32. }
    33. }
    34. package net.biancheng.c;
    35. import org.apache.commons.logging.Log;
    36. import org.apache.commons.logging.LogFactory;
    37. import org.springframework.beans.BeansException;
    38. import org.springframework.beans.factory.DisposableBean;
    39. import org.springframework.beans.factory.InitializingBean;
    40. import org.springframework.beans.factory.config.BeanPostProcessor;
    41. /**
    42. * @author
    43. * @Description bean生命周期
    44. * 1.实例化
    45. * 2.执行属性赋值
    46. * 3.BeanPostProcessor postProcessBeforeInitialization()
    47. * 4.然后执行InitializingBean afterPropertiesSet(),afterPropertiesSet()
    48. * 5.调用init-method="init"属性设置的方法名init
    49. * 6.BeanPostProcessor postProcessAfterInitialization()
    50. * 7.DisposableBean destroy
    51. * 8.调用destroy-method="destroy" 属性设置的方法名destory
    52. * @Date 2022/6/10 17:44
    53. */
    54. public class HelloWorld implements InitializingBean , BeanPostProcessor {
    55. private static final Log LOGGER = LogFactory.getLog(HelloWorld.class);
    56. private String message;
    57. public HelloWorld(String message) {
    58. this.message = message;
    59. }
    60. public HelloWorld() {
    61. System.out.println("HelloWorld()");
    62. }
    63. public void setMessage(String message) {
    64. System.out.println("message = " + message);
    65. this.message = message;
    66. }
    67. public void getMessage() {
    68. System.out.println("Message : " + message);
    69. }
    70. public void init() {
    71. this.message = "Hello World";
    72. System.out.println("Bean 正在初始化"+this.message);
    73. }
    74. public void destroy() {
    75. System.out.println("Bean 将要被销毁");
    76. }
    77. /* @Override
    78. public void destroy() {
    79. LOGGER.info(" 调用接口:DisposableBean,方法:destroy,无参数");
    80. }*/
    81. @Override
    82. public void afterPropertiesSet() {
    83. LOGGER.info("After message PropertiesSet "+this.message);
    84. }
    85. @Override
    86. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    87. System.out.println("HelloWorld Before : " + beanName);
    88. return bean;
    89. }
    90. @Override
    91. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    92. System.out.println("HelloWorld After : " + beanName);
    93. return bean;
    94. }
    95. }

    Beans.xml配置文件如下:

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="initHelloWorld" class="net.biancheng.c.InitHelloWorld" >
    6. <property name="message" value="Hello World"/>
    7. bean>
    8. <bean id="helloWorld" class="net.biancheng.c.HelloWorld"
    9. init-method="init" destroy-method="destroy">
    10. <property name="message" value="java is the best language" />
    11. bean>
    12. beans>

    执行结果
    
    
    1. Connected to the target VM, address: '127.0.0.1:10456', transport: 'socket'
    2. InitHelloWorld()
    3. message = Hello World
    4. HelloWorld()
    5. message = java is the best language
    6. InitHelloWorld Before : helloWorld
    7. 九月 18, 2022 7:52:54 下午 net.biancheng.c.HelloWorld afterPropertiesSet
    8. 信息: After message PropertiesSet java is the best language
    9. Bean 正在初始化Hello World
    10. InitHelloWorld After : helloWorld
    11. Bean 将要被销毁
    12. Disconnected from the target VM, address: '127.0.0.1:10456', transport: 'socket'
    13. Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
    14. Process finished with exit code 0
    下面进入源码分析阶段,
    ClassPathXmlApplicationContext构造函数进入refresh()。
    @Override
    public void refresh() throws BeansException, IllegalStateException {
       synchronized (this.startupShutdownMonitor) {
          // Prepare this context for refreshing.
          prepareRefresh();
    
          // Tell the subclass to refresh the internal bean factory.
          ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
          // Prepare the bean factory for use in this context.
          prepareBeanFactory(beanFactory);
    
          try {
             // Allows post-processing of the bean factory in context subclasses.
             postProcessBeanFactory(beanFactory);
    
             // Invoke factory processors registered as beans in the context.
             invokeBeanFactoryPostProcessors(beanFactory);
    
             // Register bean processors that intercept bean creation.
             //spring注册BeanPostProcessor生命周期都在此处理
             registerBeanPostProcessors(beanFactory);
    
             // Initialize message source for this context.
             initMessageSource();
    
             // Initialize event multicaster for this context.
             initApplicationEventMulticaster();
    
             // Initialize other special beans in specific context subclasses.
             onRefresh();
    
             // Check for listener beans and register them.
             registerListeners();
            //spring没有实现BeanPostProcessor接口的类的初始化
            // Instantiate all remaining (non-lazy-init) singletons.
             finishBeanFactoryInitialization(beanFactory);
    
             // Last step: publish corresponding event.
             finishRefresh();
          }
    
          catch (BeansException ex) {
             if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                      "cancelling refresh attempt: " + ex);
             }
    
             // Destroy already created singletons to avoid dangling resources.
             destroyBeans();
    
             // Reset 'active' flag.
             cancelRefresh(ex);
    
             // Propagate exception to caller.
             throw ex;
          }
    
          finally {
             // Reset common introspection caches in Spring's core, since we
             // might not ever need metadata for singleton beans anymore...
             resetCommonCaches();
          }
       }
    }

    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
       PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

    public static void registerBeanPostProcessors(
          ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
    //根据类型BeanPostProcessor获取后置处理器名称
       String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
    
       // Register BeanPostProcessorChecker that logs an info message when
       // a bean is created during BeanPostProcessor instantiation, i.e. when
       // a bean is not eligible for getting processed by all BeanPostProcessors.
       int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
       beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
    
       // Separate between BeanPostProcessors that implement PriorityOrdered,
       // Ordered, and the rest.
       List priorityOrderedPostProcessors = new ArrayList<>();
       List internalPostProcessors = new ArrayList<>();
       List orderedPostProcessorNames = new ArrayList<>();
       List nonOrderedPostProcessorNames = new ArrayList<>();
       for (String ppName : postProcessorNames) {
          if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
             BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
             priorityOrderedPostProcessors.add(pp);
             if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
             }
          }
          else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
             orderedPostProcessorNames.add(ppName);
          }
          else {
             nonOrderedPostProcessorNames.add(ppName);
          }
       }
    
       // First, register the BeanPostProcessors that implement PriorityOrdered.
       sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
       registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
    
       // Next, register the BeanPostProcessors that implement Ordered.
       List orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
       for (String ppName : orderedPostProcessorNames) {
          BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
          orderedPostProcessors.add(pp);
          if (pp instanceof MergedBeanDefinitionPostProcessor) {
             internalPostProcessors.add(pp);
          }
       }
       sortPostProcessors(orderedPostProcessors, beanFactory);
       registerBeanPostProcessors(beanFactory, orderedPostProcessors);
    
       // Now, register all regular BeanPostProcessors.
       List nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
       for (String ppName : nonOrderedPostProcessorNames) {
          BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
          nonOrderedPostProcessors.add(pp);
          if (pp instanceof MergedBeanDefinitionPostProcessor) {
             internalPostProcessors.add(pp);
          }
       }
       registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
    
       // Finally, re-register all internal BeanPostProcessors.
       sortPostProcessors(internalPostProcessors, beanFactory);
       registerBeanPostProcessors(beanFactory, internalPostProcessors);
    
       // Re-register post-processor for detecting inner beans as ApplicationListeners,
       // moving it to the end of the processor chain (for picking up proxies etc).
       beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    }
    

    核心在doCreateBean()方法。代码如下:

    AbstractAutowireCapableBeanFactory#doCreateBean()
    protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
          throws BeanCreationException {
    
       // Instantiate the bean.
       BeanWrapper instanceWrapper = null;
       if (mbd.isSingleton()) {
          instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
       }
       if (instanceWrapper == null) {
    //第一步 实例化
          instanceWrapper = createBeanInstance(beanName, mbd, args);
       }
       final Object bean = instanceWrapper.getWrappedInstance();
       Class beanType = instanceWrapper.getWrappedClass();
       if (beanType != NullBean.class) {
          mbd.resolvedTargetType = beanType;
       }
    
       // Allow post-processors to modify the merged bean definition.
       synchronized (mbd.postProcessingLock) {
          if (!mbd.postProcessed) {
             try {
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
             }
             catch (Throwable ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                      "Post-processing of merged bean definition failed", ex);
             }
             mbd.postProcessed = true;
          }
       }
    
       // Eagerly cache singletons to be able to resolve circular references
       // even when triggered by lifecycle interfaces like BeanFactoryAware.
       boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
             isSingletonCurrentlyInCreation(beanName));
       if (earlySingletonExposure) {
          if (logger.isTraceEnabled()) {
             logger.trace("Eagerly caching bean '" + beanName +
                   "' to allow for resolving potential circular references");
          }
          addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
       }
    
       // Initialize the bean instance.
       Object exposedObject = bean;
       try {
    //第二步填充属性值
          populateBean(beanName, mbd, instanceWrapper);
    //第三步初始化初始化(
    1、PostBeanProcessor#postProcessBeforeInitialization()方法
    2、InitializingBean#afterPropertiesSet(),afterPropertiesSet()先于init()方法
    3、BeanPostProcessor#postProcessAfterInitialization()方法
    ) 
          exposedObject = initializeBean(beanName, exposedObject, mbd);
       }
       catch (Throwable ex) {
          if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
             throw (BeanCreationException) ex;
          }
          else {
             throw new BeanCreationException(
                   mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
          }
       }
    
       if (earlySingletonExposure) {
          Object earlySingletonReference = getSingleton(beanName, false);
          if (earlySingletonReference != null) {
             if (exposedObject == bean) {
                exposedObject = earlySingletonReference;
             }
             else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                String[] dependentBeans = getDependentBeans(beanName);
                Set actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
                for (String dependentBean : dependentBeans) {
                   if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                      actualDependentBeans.add(dependentBean);
                   }
                }
                if (!actualDependentBeans.isEmpty()) {
                   throw new BeanCurrentlyInCreationException(beanName,
                         "Bean with name '" + beanName + "' has been injected into other beans [" +
                         StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                         "] in its raw version as part of a circular reference, but has eventually been " +
                         "wrapped. This means that said other beans do not use the final version of the " +
                         "bean. This is often the result of over-eager type matching - consider using " +
                         "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
                }
             }
          }
       }
    
       // Register bean as disposable.
    //第四步 销毁
       try {
          registerDisposableBeanIfNecessary(beanName, bean, mbd);
       }
       catch (BeanDefinitionValidationException ex) {
          throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
       }
    
       return exposedObject;
    }

    //第一步 实例化

    /**
     * Create a new instance for the specified bean, using an appropriate instantiation strategy:
     * factory method, constructor autowiring, or simple instantiation.
     * @param beanName the name of the bean
     * @param mbd the bean definition for the bean
     * @param args explicit arguments to use for constructor or factory method invocation
     * @return a BeanWrapper for the new instance
     * @see #obtainFromSupplier
     * @see #instantiateUsingFactoryMethod
     * @see #autowireConstructor
     * @see #instantiateBean
     */
    protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
       // Make sure bean class is actually resolved at this point.
       Class beanClass = resolveBeanClass(mbd, beanName);
    
       if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
          throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
       }
    
       Supplier instanceSupplier = mbd.getInstanceSupplier();
       if (instanceSupplier != null) {
          return obtainFromSupplier(instanceSupplier, beanName);
       }
    
       if (mbd.getFactoryMethodName() != null) {
          return instantiateUsingFactoryMethod(beanName, mbd, args);
       }
    
       // Shortcut when re-creating the same bean...
       boolean resolved = false;
       boolean autowireNecessary = false;
       if (args == null) {
          synchronized (mbd.constructorArgumentLock) {
             if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                autowireNecessary = mbd.constructorArgumentsResolved;
             }
          }
       }
       if (resolved) {
          if (autowireNecessary) {
             return autowireConstructor(beanName, mbd, null, null);
          }
          else {
             return instantiateBean(beanName, mbd);
          }
       }
    
       // Candidate constructors for autowiring?
       Constructor[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
       if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
             mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
          return autowireConstructor(beanName, mbd, ctors, args);
       }
    
       // Preferred constructors for default construction?
       ctors = mbd.getPreferredConstructors();
       if (ctors != null) {
          return autowireConstructor(beanName, mbd, ctors, null);
       }
    
       // No special handling: simply use no-arg constructor.
       //无参构造方法实例化
       return instantiateBean(beanName, mbd);
    
    }

    /**
     * Instantiate the given bean using its default constructor.
     * @param beanName the name of the bean
     * @param mbd the bean definition for the bean
     * @return a BeanWrapper for the new instance
     */
    protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
       try {
          Object beanInstance;
          final BeanFactory parent = this;
          if (System.getSecurityManager() != null) {
             beanInstance = AccessController.doPrivileged((PrivilegedAction) () ->
                   getInstantiationStrategy().instantiate(mbd, beanName, parent),
                   getAccessControlContext());
          }
          else {
    //真正无参构造函数实例化
             beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
          }
          BeanWrapper bw = new BeanWrapperImpl(beanInstance);
          initBeanWrapper(bw);
          return bw;
       }
       catch (Throwable ex) {
          throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
       }
    }
     
    

    //第二步属性赋值

    /**
     * Apply the given property values, resolving any runtime references
     * to other beans in this bean factory. Must use deep copy, so we
     * don't permanently modify this property.
     * @param beanName the bean name passed for better exception information
     * @param mbd the merged bean definition
     * @param bw the BeanWrapper wrapping the target object
     * @param pvs the new property values
     */
    protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
       if (pvs.isEmpty()) {
          return;
       }
    
       if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
          ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
       }
    
       MutablePropertyValues mpvs = null;
       List original;
    
       if (pvs instanceof MutablePropertyValues) {
          mpvs = (MutablePropertyValues) pvs;
          if (mpvs.isConverted()) {
             // Shortcut: use the pre-converted values as-is.
             try {
                bw.setPropertyValues(mpvs);
                return;
             }
             catch (BeansException ex) {
                throw new BeanCreationException(
                      mbd.getResourceDescription(), beanName, "Error setting property values", ex);
             }
          }
          original = mpvs.getPropertyValueList();
       }
       else {
          original = Arrays.asList(pvs.getPropertyValues());
       }
    
       TypeConverter converter = getCustomTypeConverter();
       if (converter == null) {
          converter = bw;
       }
       BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
    
       // Create a deep copy, resolving any references for values.
       List deepCopy = new ArrayList<>(original.size());
       boolean resolveNecessary = false;
       for (PropertyValue pv : original) {
          if (pv.isConverted()) {
             deepCopy.add(pv);
          }
          else {
             String propertyName = pv.getName();
             Object originalValue = pv.getValue();
             if (originalValue == AutowiredPropertyMarker.INSTANCE) {
                Method writeMethod = bw.getPropertyDescriptor(propertyName).getWriteMethod();
                if (writeMethod == null) {
                   throw new IllegalArgumentException("Autowire marker for property without write method: " + pv);
                }
                originalValue = new DependencyDescriptor(new MethodParameter(writeMethod, 0), true);
             }
             Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
             Object convertedValue = resolvedValue;
             boolean convertible = bw.isWritableProperty(propertyName) &&
                   !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
             if (convertible) {
                convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
             }
             // Possibly store converted value in merged bean definition,
             // in order to avoid re-conversion for every created bean instance.
             if (resolvedValue == originalValue) {
                if (convertible) {
                   pv.setConvertedValue(convertedValue);
                }
                deepCopy.add(pv);
             }
             else if (convertible && originalValue instanceof TypedStringValue &&
                   !((TypedStringValue) originalValue).isDynamic() &&
                   !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
                pv.setConvertedValue(convertedValue);
                deepCopy.add(pv);
             }
             else {
                resolveNecessary = true;
                deepCopy.add(new PropertyValue(pv, convertedValue));
             }
          }
       }
       if (mpvs != null && !resolveNecessary) {
          mpvs.setConverted();
       }
    //第二步 属性赋值
       // Set our (possibly massaged) deep copy.
       try {
          bw.setPropertyValues(new MutablePropertyValues(deepCopy));
       }
       catch (BeansException ex) {
          throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Error setting property values", ex);
       }
    }

    //第三步 初始化

    /**
     * Initialize the given bean instance, applying factory callbacks
     * as well as init methods and bean post processors.
     * 

    Called from {@link #createBean} for traditionally defined beans, * and from {@link #initializeBean} for existing bean instances. * @param beanName the bean name in the factory (for debugging purposes) * @param bean the new bean instance we may need to initialize * @param mbd the bean definition that the bean was created with * (can also be {@code null}, if given an existing bean instance) * @return the initialized bean instance (potentially wrapped) * @see BeanNameAware * @see BeanClassLoaderAware * @see BeanFactoryAware * @see #applyBeanPostProcessorsBeforeInitialization * @see #invokeInitMethods * @see #applyBeanPostProcessorsAfterInitialization */ protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //调用BeanPostProcessors的postProcessBeforeInitialization方法 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //调用init方法(先执行InitializingBean#afterPropertiesSet()方法、然后执行xml定义的init-method="init" init方法) invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { //调用BeanPostProcessors的postProcessAfterInitialization方法 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }

    /**
     * Give a bean a chance to react now all its properties are set,
     * and a chance to know about its owning bean factory (this object).
     * This means checking whether the bean implements InitializingBean or defines
     * a custom init method, and invoking the necessary callback(s) if it does.
     * @param beanName the bean name in the factory (for debugging purposes)
     * @param bean the new bean instance we may need to initialize
     * @param mbd the merged bean definition that the bean was created with
     * (can also be {@code null}, if given an existing bean instance)
     * @throws Throwable if thrown by init methods or by the invocation process
     * @see #invokeCustomInitMethod
     */
    protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
          throws Throwable {
    
       boolean isInitializingBean = (bean instanceof InitializingBean);
       if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
          if (logger.isTraceEnabled()) {
             logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
          }
          if (System.getSecurityManager() != null) {
             try {
                AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
                   ((InitializingBean) bean).afterPropertiesSet();
                   return null;
                }, getAccessControlContext());
             }
             catch (PrivilegedActionException pae) {
                throw pae.getException();
             }
          }
          else {
             ((InitializingBean) bean).afterPropertiesSet();
          }
       }
    
       if (mbd != null && bean.getClass() != NullBean.class) {
          String initMethodName = mbd.getInitMethodName();
          if (StringUtils.hasLength(initMethodName) &&
                !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                !mbd.isExternallyManagedInitMethod(initMethodName)) {
             invokeCustomInitMethod(beanName, bean, mbd); //自定义init方法init-method="init"
          }
       }
    } 
    

    总结:生命周期经历4个阶段

    1、实例化

    2、属性赋值

    3、初始化

    4、销毁

    调式跟踪。。。 如果只是看别人博客自己不调试,可能过段时间就忘记了。一定要多动手实践!!!

  • 相关阅读:
    MySql中事务详细理解学习(事务管理、事务隔离级别、事务传播机制)
    贪心算法C++
    修改npm全局安装模式的路径
    C Primer Plus(6) 中文版 第11章 字符串和字符串函数 11.6 字符串示例:字符串排序
    es6两个数组取交集、并集、差集、补集
    线性代数学习笔记6-3:行列式的计算、代数余子式
    开源项目-十六进制协议传感器自适应缩放曲线显示终端(百问网imx6ull & 小熊派结合)...
    【0205】哪些信号可kill掉postmaster?内核如何实现?
    HTTP 常⻅的状态码有哪些,以及适⽤场景
    【毕业设计】57-基于单片机的超声波测距仿真倒车雷达系统设计(源程序+原理图工程+PCB+仿真工程+答辩论文)
  • 原文地址:https://blog.csdn.net/SOMECT/article/details/126921311