1、初始化流程java
spring容器啓動時,會實例化非懶加載的bean實例,將bean實例交給spring容器管理,避免程序中經過new關鍵字建立。bean的初始化主要分爲4個步驟:1)從緩存中查找;2)建立bean實例並完成自動裝配;3)執行初始化方法;4)將bean放入緩存。具體流程如圖所示:spring
1)從緩存map中以beanName爲key查找bean實例,若存在,則返回緩存中的實例;緩存
2)判斷當前spring容器是否加載了該benaName對應的BeanDefinition,若不存在,則交給父容器處理(如spring爲spring mvc的父容器);mvc
3)合併BeanDefinition,遞歸調用,從父bean中繼承PropertyValues、自動裝配方式等,此步驟後,bean中的PropertyValues包含父bean中定義的PropertyValues;app
4)執行InstantiationAwareBeanPostProcessor擴展點的前置方法,若前置方法返回的對象不爲空,則執行BeanPostProcessor的後置方法,返回前置方法返回的實例,並將器緩存到spring容器(如實現AOP的關鍵類AbstractAutoProxyCreator);ide
5)建立BeanWrapper實例(Bean的裝飾類),先判斷是否配置factory-method工廠方法,若是有配置,則調用工廠方法建立BeanWrapper,不然調用SmartInstantiationAwareBeanPostProcessor擴展點的determineCandidateConstructors()方法找到類的構造方法,經過反射建立BeanWrapper實例;函數
6)執行MergedBeanDefinitionPostProcessor擴展點的postProcessMergedBeanDefinition()方法,加載類中@Autowired、@Value、@PostConstruct等註解,並將其放入緩存;源碼分析
7)若是支持循環引用,則將未完成裝配等bean實例放入第三級緩存(解決循環依賴);post
8)自動裝配,填充依賴,此時裝配的是PropertyValues中的屬性,@Autowired、@Value裝配不在此步驟執行;ui
9)執行初始化方法,先執行Aware接口的方法,再執行BeanPostProcessor擴展點的postProcessBeforeInitialization()前置方法,如@Autowired、@Value、@PostConstruct,再執行InitializingBean接口的afterPropertiesSet()方法,最後執行init-method指定的方法;
10)執行BeanPostProcessor擴展點的後置方法;
11)將bean實例方法spring容器緩存。
2、bean初始化源碼分析
spring容器刷新上下文事件時,當定義得bean元數據及BeanPostProcessor初始化完成後,便會對非懶加載對bean進行初始化。
1)AbstractApplicationContext#refresh()刷新上下文方法:
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. 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(); // 初始化非懶加載對bean. 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 finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(new StringValueResolver() { @Override public String resolveStringValue(String strVal) { return getEnvironment().resolvePlaceholders(strVal); } }); } String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. beanFactory.freezeConfiguration(); // 真正初始化非懶加載對bean實例 beanFactory.preInstantiateSingletons(); }
2)DefaultListableBeanFactory#preInstantiateSingletons()初始化非懶加載對bean:
public void preInstantiateSingletons() throws BeansException { if (this.logger.isDebugEnabled()) { this.logger.debug("Pre-instantiating singletons in " + this); } List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... // 循環spring容器中對bean for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); // 非抽象類&單例&非懶加載 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { // 若是bean是FactoryBean,則爲beanName加上前綴"&",表示獲取FactoryBean自己 if (isFactoryBean(beanName)) { final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName); boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() @Override public Boolean run() { return ((SmartFactoryBean<?>) factory).isEagerInit(); } }, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } else { // 實例化Bean getBean(beanName); } } }
3)AbstractBeanFactory#doGetBean(),該方法會先從緩存中查找bean,若存在,則返回緩存中的bean,不然走實例化bean流程
protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. // 從緩存中查找bean,若緩存中存在,則返回緩存中的bean,此處也是解決循環依賴的方法 Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { BeanFactory parentBeanFactory = getParentBeanFactory(); // 到父容器查找 if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { // 合併BeanDefinition,如設置了<bean parent="xxx">的場景,使用遞歸方法從父BeanDefinition中 // 繼承PropertyValues等,此步驟後,bean中的PropertyValues包含父bean中定義的PropertyValues final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // 省略部分代碼 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { try { // 建立bean return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } } }); // 該方法主要是針對FactoryBean,若是是FactoryBean,則調用getObject() bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } // 省略非單例對bean對實例化代碼 catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // 省略部分代碼 return (T) bean; }
4)AbstractAutowireCapableBeanFactory#createBean(),該方法會先交給實現了InstantiationAwareBeanPostProcessor接口的擴展點一個返回bean實例的機會
protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) { RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // Prepare method overrides. try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // 交給BeanPostProcessor擴展點執行,當返回等對象非null時,則bean初始化流程終止,如aop等代理對象 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; }
5)AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation(),獲得spring容器中實現了InstantiationAwareBeanPostProcessor擴展點的類,並執行擴展點的前置方法,若前置方法返回的對象不是null,則執行後置方法,且中斷該bean的初始化流程
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { // Make sure bean class is actually resolved at this point. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null) { // 執行實現了InstantiationAwareBeanPostProcessor接口的前置方法 bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); // 返回bean不爲空時,執行實現了InstantiationAwareBeanPostProcessor接口的後置方法 if (bean != null) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } mbd.beforeInstantiationResolved = (bean != null); } return bean; } // BeanPostProcessor前置方法 protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null) { return result; } } } return null; } // BeanPostProcessor後置方法 public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessAfterInitialization(result, beanName); if (result == null) { return result; } } return result; }
6)AbstractAutowireCapableBeanFactory#doCreateBean(),該方法是bean實例化的核心,會先建立未完成裝配功能的bean,而後將bean放入緩存,接着實現自動裝配,最後調用初始化方法,完成初始化bean的整個流程
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { // 經過BeanWrapper實例化Bean BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { // 若是在bean定義中指定了factory-method,則執行工廠方法返回bean,不然會根據 // SmartInstantiationAwareBeanPostProcessor擴展點的determineCandidateConstructors()方法, // 找到對應的構造函數,經過反射建立bean,可看源碼瞭解具體實現思路 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); mbd.resolvedTargetType = beanType; // 執行MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition流程,將@Resource、 // @Autowired、@Value等註解加載到緩存,供後續使用 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; } } // 當bean是單例,並容許循環依賴,且bean的狀態是建立狀態,則將未完成裝配的bean放入第三級緩存,解決循環依賴 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } addSingletonFactory(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } // Initialize the bean instance. Object exposedObject = bean; try { // 完成bean的裝配 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { // 調用初始化方法,完成bean的初始化 exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { } // 省略部分代碼 return exposedObject; }
7)AbstractAutowireCapableBeanFactory#createBeanInstance(),該方法是經過指定的工廠bean的方法或反射建立bean
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, 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()); } // 若是指定了factory-method屬性,則調用工廠方法建立bean if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same bean... boolean resolved = false; // bean配置中是否指定了需經過帶參數的構造函數建立bean boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } if (resolved) { if (autowireNecessary) { // 根據配置的構造函數參數建立bean return autowireConstructor(beanName, mbd, null, null); } else { // 反射,調用默認構造函數建立bean return instantiateBean(beanName, mbd); } } // 若是是經過用戶自定義的需建立帶參數的構造函數bean,則執行SmartInstantiationAwareBeanPostProcessor // 接口的determineCandidateConstructors()方法,找到對應的構造函數 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // No special handling: simply use no-arg constructor. return instantiateBean(beanName, mbd); }
8)AbstractAutowireCapableBeanFactory#populateBean(),該方法主要是完成bean的自動裝配,自動裝配的方法主要有:AUTOWIRE_NO、AUTOWIRE_BY_NAME、AUTOWIRE_BY_TYPE、AUTOWIRE_CONSTRUCTOR這4種,默認是AUTOWIRE_NO不自動裝配,需在bean定義中配置完成屬性注入
protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) { PropertyValues pvs = mbd.getPropertyValues(); // 省略部分代碼 // 自動裝配,默認值爲AUTOWIRE_NO if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); if (hasInstAwareBpps) { // 執行InstantiationAwareBeanPostProcessor的postProcessPropertyValues()方法,將bean中 // 的@Autowired、@Value、@Resource註解經過反射的方式,注入到bean中 for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { checkDependencies(beanName, mbd, filteredPds, pvs); } } // 將PropertyValues集合的值注入到bean中對應的屬性中,完成屬性裝配 applyPropertyValues(beanName, mbd, bw, pvs); }
9)AbstractAutowireCapableBeanFactory#initializeBean(),bean建立後的初始化方法,包括執行Aware接口方法、@PostConstruct、afterProperties()及init-method方法
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { invokeAwareMethods(beanName, bean); return null; } }, getAccessControlContext()); } else { // Aware接口方法 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { // BeanPostProcessor前置方法,如@PostConstruct註解方法 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { // 初始化方法 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()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); // 執行InitializingBean的afterPropertiesSet() if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { ((InitializingBean) bean).afterPropertiesSet(); return null; } }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { ((InitializingBean) bean).afterPropertiesSet(); } } // 執行自定義的init-method方法 if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } }
3、BeanPostProcessor擴展點
BeanPostProcessor擴展點有五個接口十個實現,具體以下圖所示:
1)InstantiationAwareBeanPostProcessor:實例化bean的擴展點,Spring自帶的實現類有CommonAnnotationBeanPostProcessor、AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor等
1.1)postProcessBeforeInstantiation方法:在實例化目標對象以前執行,可自定義實例化邏輯,AOP返回的對象是AbstractAutoProxyCreator返回的代理對象;
1.2)postProcessAfterInstantiation方法:bean實例化完成後的操做,若是返回false,將阻止其餘的InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation執行;
1.3)postProcessPropertyValues方法:完成其餘的定製的一些依賴注入,如AutowiredAnnotationBeanPostProcessor執行@Autowired、@Value註解,CommonAnnotationBeanPostProcessor執行@Resource等註解的注入。
2)MergedBeanDefinitionPostProcessor:合併bean定義的擴展點,Spring自帶的實現類有CommonAnnotationBeanPostProcessor、AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor等
2.1)postProcessMergedBeanDefinition方法:執行bean定義的合併,如將@Autowired、@Value等註解加載到緩存,供依賴注入時使用。
3)SmartInstantiationAwareBeanPostProcessor:繼承InstantiationAwareBeanPostProcessor,在InstantiationAwareBeanPostProcessor接口基礎上增長了3個方法,Spring自帶的實現類有AspectJAwareAdvisorAutoProxyCreator、AnnotationAwareAspectJAutoProxyCreator等:
3.1)predictBeanType方法:預測Bean的類型,返回第一個預測成功的Class類型,若是不能預測返回null;調用BeanFactory.getType(name)時,經過Bean定義沒法獲得Bean類型信息時就調用該回調方法來決定類型信息;BeanFactory.isTypeMatch(name, targetType)用於檢測給定名字的Bean是否匹配目標類型(如在依賴注入時須要使用);
3.2)determineCandidateConstructors方法:檢測Bean的構造器,能夠檢測出多個候選構造器,再根據參數決定選擇哪個構造器,在反射建立BeanWrapper步驟使用;
3.3)getEarlyBeanReference方法:獲取提前暴露的對象,即spring爲了解決循環依賴,在未完成屬性裝配和bean初始化方法以前,就不完整的bean放入第三級緩存。
4) DestructionAwareBeanPostProcessor:銷燬bean的擴展點,spring自帶的實現類有InitDestroyAnnotationBeanPostProcessor:
4.1)postProcessBeforeDestruction方法:銷燬後處理回調方法,該回調只能應用到單例Bean,如InitDestroyAnnotationBeanPostProcessor完成@PreDestroy註解的銷燬方法調用。
5)BeanPostProcessor擴展點:該擴展點是以上4個擴展點接口的父接口,自定義的擴展點通常是實現改接口
5.1)postProcessBeforeInitialization方法:在實例化、依賴注入後,調用InitializingBean的afterPropertiesSet()方法以前執行,如@PostConstruct註解方法等,方法的返回值將替代原始的Bean對象;
5.2)postProcessAfterInitialization方法:在實例化、依賴注入、初始化完成後執行,方法的返回值將替代原始的Bean對象。