spring源碼-bean之加載-2

  1、前面說了bean的容器初始化,後面固然是說bean的加載。這裏仍是不講解ApplicationContext的bean的加載過程,仍是經過最基礎的XmlBeanFactory來進行講解,主要是熟悉經過BeanFactory是怎麼實現class的實例化的。spring

  2、聲明一下:XmlBeanFactory和ApplicationContext的不一樣在與,ApplicationContext在容器初始化的時候,就作了不少準備操做。而XmlBeanFactory是在獲取bean的時候纔會調用初始化的東西。這一點是XmlBeanFactory作的很差的地方。好比:BeanPostProcessor、InitializingBean和標籤init-method等,這些都是在bean加載的時候完成而不是在初始化的時候。app

  3、bean的加載源碼解讀ide

  1)bean的獲取方式post

package com.pinnet.bean;

import com.pinnet.customLabel.User;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

    public static void main(String[] args) {
        //讀取資源
        Resource resource = new ClassPathResource("spring-bean.xml");
        //初始化容器
        XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
        //獲取對應的bean
        xmlBeanFactory.getBean("user", User.class);
    }
}

  2)getBeanui

   public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return this.doGetBean(name, requiredType, (Object[])null, false);
    }
    protected <T> T doGetBean(String name, Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {
        //檢查是否存在別名,採用存在別名的方式
        final String beanName = this.transformedBeanName(name);
        //經過單例的map來獲取實例,固然這個第一次是沒有的,是在加載事後放入到單例的map裏面
        Object sharedInstance = this.getSingleton(beanName);
        Object bean;
        if (sharedInstance != null && args == null) {
            if (this.logger.isDebugEnabled()) {
                if (this.isSingletonCurrentlyInCreation(beanName)) {
                    this.logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference");
                } else {
                    this.logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            //若是存在bean的話,看是不是FactoryBean,若是是則經過factoryBean的方式加載
            bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, (RootBeanDefinition)null);
        } else {
            if (this.isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }
            //若是存在父類bean的話,現價在父類
            BeanFactory parentBeanFactory = this.getParentBeanFactory();
            if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) {
                String nameToLookup = this.originalBeanName(name);
                if (args != null) {
                    return parentBeanFactory.getBean(nameToLookup, args);
                }

                return parentBeanFactory.getBean(nameToLookup, requiredType);
            }
            if (!typeCheckOnly) {
                this.markBeanAsCreated(beanName);
            }
            //獲取在容器裏面初始化的BeanDefinition
            final RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
            this.checkMergedBeanDefinition(mbd, beanName, args);
            String[] dependsOn = mbd.getDependsOn();
            String scopeName;
            if (dependsOn != null) {
                String[] var14 = dependsOn;
                int var13 = dependsOn.length;
                for(int var12 = 0; var12 < var13; ++var12) {
                    scopeName = var14[var12];
                    this.getBean(scopeName);
                    this.registerDependentBean(scopeName, beanName);
                }
            }

            //全部配置默認是爲單例的
            if (mbd.isSingleton()) {
                sharedInstance = this.getSingleton(beanName, new ObjectFactory() {
                    public Object getObject() throws BeansException {
                        try {
                            //這裏是整個bean加載的核心
                            return AbstractBeanFactory.this.createBean(beanName, mbd, args);
                        } catch (BeansException var2) {
                            AbstractBeanFactory.this.destroySingleton(beanName);
                            throw var2;
                        }
                    }
                });
                //若是存在bean的話,看是不是FactoryBean,若是是則經過factoryBean的方式加載
                bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            } else if (mbd.isPrototype()) {
                scopeName = null;
                Object prototypeInstance;
                try {
                    this.beforePrototypeCreation(beanName);
                    prototypeInstance = this.createBean(beanName, mbd, args);
                } finally {
                    this.afterPrototypeCreation(beanName);
                }
                //若是存在bean的話,看是不是FactoryBean,若是是則經過factoryBean的方式加載
                bean = this.getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            } else {
                scopeName = mbd.getScope();
                Scope scope = (Scope)this.scopes.get(scopeName);
                if (scope == null) {
                    throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
                }
                try {
                    Object scopedInstance = scope.get(beanName, new ObjectFactory() {
                        public Object getObject() throws BeansException {
                            AbstractBeanFactory.this.beforePrototypeCreation(beanName);
                            Object var2;
                            try {
                                var2 = AbstractBeanFactory.this.createBean(beanName, mbd, args);
                            } finally {
                                AbstractBeanFactory.this.afterPrototypeCreation(beanName);
                            }
                            return var2;
                        }
                    });
                    bean = this.getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                } catch (IllegalStateException var19) {
                    throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; " + "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", var19);
                }
            }
        }

        if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
            try {
                return this.getTypeConverter().convertIfNecessary(bean, requiredType);
            } catch (TypeMismatchException var20) {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Failed to convert bean '" + name + "' to required type [" + ClassUtils.getQualifiedName(requiredType) + "]", var20);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        } else {
            return bean;
        }
    }

  3)getSingleton、getMergedLocalBeanDefinition、createBeanthis

  a、getSingletonspa

public Object getSingleton(String beanName) {
        return this.getSingleton(beanName, true);
    }

    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        //singletonObjects是一個map,主要是目的是存放單例bean
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {
            Map var4 = this.singletonObjects;
            synchronized(this.singletonObjects) {
                //若是singletonObjects沒有,則在earlySingletonObjects去找
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
                    ObjectFactory singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
                    if (singletonFactory != null) {
                        singletonObject = singletonFactory.getObject();
                        this.earlySingletonObjects.put(beanName, singletonObject);
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }
        return singletonObject != NULL_OBJECT ? singletonObject : null;
    }

  b、getMergedLocalBeanDefinitionprototype

   protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
        //默認在mergedBeanDefinitions(map)裏面查找
        RootBeanDefinition mbd = (RootBeanDefinition)this.mergedBeanDefinitions.get(beanName);
        //注意這裏的getBeanDefinition,這裏是從beanDefinitionMap裏面取查找的,整個在容器加載的時候說明了
        return mbd != null ? mbd : this.getMergedBeanDefinition(beanName, this.getBeanDefinition(beanName));
    }

    protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeanDefinitionStoreException {
        return this.getMergedBeanDefinition(beanName, bd, (BeanDefinition)null);
    }

    protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException {
        Map var4 = this.mergedBeanDefinitions;
        synchronized(this.mergedBeanDefinitions) {
            RootBeanDefinition mbd = null;
            if (containingBd == null) {
                mbd = (RootBeanDefinition)this.mergedBeanDefinitions.get(beanName);
            }

            if (mbd == null) {
                if (bd.getParentName() == null) {
                    if (bd instanceof RootBeanDefinition) {
                        mbd = ((RootBeanDefinition)bd).cloneBeanDefinition();
                    } else {
                        //將其餘類型BeanDefinition包裝成RootBeanDefinition
                        mbd = new RootBeanDefinition(bd);
                    }
                } else {
                    //父類bean不爲空的操做
                    BeanDefinition pbd;
                    try {
                        String parentBeanName = this.transformedBeanName(bd.getParentName());
                        if (!beanName.equals(parentBeanName)) {
                            pbd = this.getMergedBeanDefinition(parentBeanName);
                        } else {
                            if (!(this.getParentBeanFactory() instanceof ConfigurableBeanFactory)) {
                                throw new NoSuchBeanDefinitionException(bd.getParentName(), "Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent");
                            }

                            pbd = ((ConfigurableBeanFactory)this.getParentBeanFactory()).getMergedBeanDefinition(parentBeanName);
                        }
                    } catch (NoSuchBeanDefinitionException var8) {
                        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", var8);
                    }

                    mbd = new RootBeanDefinition(pbd);
                    mbd.overrideFrom(bd);
                }
                //這裏默認設置成單例的
                if (!StringUtils.hasLength(mbd.getScope())) {
                    mbd.setScope("singleton");
                }
                if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                    mbd.setScope(containingBd.getScope());
                }
                if (containingBd == null && this.isCacheBeanMetadata() && this.isBeanEligibleForMetadataCaching(beanName)) {
                    this.mergedBeanDefinitions.put(beanName, mbd);
                }
            }
            return mbd;
        }
    }
    public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
        //很少解釋,這裏在容器初始化的時候進行操做
        BeanDefinition bd = (BeanDefinition)this.beanDefinitionMap.get(beanName);
        if (bd == null) {
            if (this.logger.isTraceEnabled()) {
                this.logger.trace("No bean named '" + beanName + "' found in " + this);
            }

            throw new NoSuchBeanDefinitionException(beanName);
        } else {
            return bd;
        }
    }

  c、getSingleton(這裏實在調用createBean的地方)debug

public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
        Assert.notNull(beanName, "'beanName' must not be null");
        Map var3 = this.singletonObjects;
        synchronized(this.singletonObjects) {
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null) {
                if (this.singletonsCurrentlyInDestruction) {
                    throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)");
                }
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
                }
                //建立bean以前作的操做
                this.beforeSingletonCreation(beanName);
                boolean recordSuppressedExceptions = this.suppressedExceptions == null;
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = new LinkedHashSet();
                }
                try {
                    //這裏調用createBean
                    singletonObject = singletonFactory.getObject();
                } catch (BeanCreationException var13) {
                    BeanCreationException ex = var13;
                    if (recordSuppressedExceptions) {
                        Iterator var8 = this.suppressedExceptions.iterator();

                        while(var8.hasNext()) {
                            Exception suppressedException = (Exception)var8.next();
                            ex.addRelatedCause(suppressedException);
                        }
                    }
                    throw ex;
                } finally {
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = null;
                    }
                    this.afterSingletonCreation(beanName);
                }
                //添加到單例的map裏面(這裏不細講解)
                this.addSingleton(beanName, singletonObject);
            }
            return singletonObject != NULL_OBJECT ? singletonObject : null;
        }
    }

  d、createBeancode

    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Creating instance of bean '" + beanName + "'");
        }
        //實例化beanClass,放在mbd的beanClass下面,目前當作class被實例化在beanClass下
        this.resolveBeanClass(mbd, beanName, new Class[0]);
        try {
            //準備重寫的方法,很少介紹
            mbd.prepareMethodOverrides();
        } catch (BeanDefinitionValidationException var5) {
            throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", var5);
        }
        Object beanInstance;
        try {
            //處理InstantiationAwareBeanPostProcessor下的調用,該結構繼承BeanPostProcessor
            beanInstance = this.resolveBeforeInstantiation(beanName, mbd);
            if (beanInstance != null) {
                return beanInstance;
            }
        } catch (Throwable var6) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", var6);
        }
        //這裏主要是作一些初始化的操做等
        beanInstance = this.doCreateBean(beanName, mbd, args);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Finished creating instance of bean '" + beanName + "'");
        }
        return beanInstance;
    }

  d.1 resolveBeanClass

    protected Class resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class... typesToMatch) throws CannotLoadBeanClassException {
        try {
            if (mbd.hasBeanClass()) {
                return mbd.getBeanClass();
            } else {
                return System.getSecurityManager() != null ? (Class) AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
                    public Class run() throws Exception {
                        return AbstractBeanFactory.this.doResolveBeanClass(mbd, typesToMatch);
                    }
                //不存在beanClass的時候調用    
                }, this.getAccessControlContext()) : this.doResolveBeanClass(mbd, typesToMatch);
            }
        } catch (PrivilegedActionException var6) {
            ClassNotFoundException ex = (ClassNotFoundException)var6.getException();
            throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
        } catch (ClassNotFoundException var7) {
            throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), var7);
        } catch (LinkageError var8) {
            throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), var8);
        }
    }
    private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) throws ClassNotFoundException {
        if (!ObjectUtils.isEmpty(typesToMatch)) {
            ClassLoader tempClassLoader = this.getTempClassLoader();
            if (tempClassLoader != null) {
                if (tempClassLoader instanceof DecoratingClassLoader) {
                    DecoratingClassLoader dcl = (DecoratingClassLoader)tempClassLoader;
                    Class[] var8 = typesToMatch;
                    int var7 = typesToMatch.length;
                    for(int var6 = 0; var6 < var7; ++var6) {
                        Class<?> typeToMatch = var8[var6];
                        dcl.excludeClass(typeToMatch.getName());
                    }
                }
                String className = mbd.getBeanClassName();
                return className != null ? ClassUtils.forName(className, tempClassLoader) : null;
            }
        }
        //這裏應爲typesToMatch不爲空,因此走這裏
        return mbd.resolveBeanClass(this.getBeanClassLoader());
    }
    public Class resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
        String className = this.getBeanClassName();
        if (className == null) {
            return null;
        } else {
            //經過反射建立實例
            Class resolvedClass = ClassUtils.forName(className, classLoader);
            this.beanClass = resolvedClass;
            return resolvedClass;
        }
    }

  d.2 resolveBeforeInstantiation(很少解釋)

    protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
        Object bean = null;
        if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
            if (mbd.hasBeanClass() && !mbd.isSynthetic() && this.hasInstantiationAwareBeanPostProcessors()) {
                bean = this.applyBeanPostProcessorsBeforeInstantiation(mbd.getBeanClass(), beanName);
                if (bean != null) {
                    bean = this.applyBeanPostProcessorsAfterInitialization(bean, beanName);
                }
            }

            mbd.beforeInstantiationResolved = bean != null;
        }

        return bean;
    }

  d.3 doCreateBean

   protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, Object[] args) {
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            instanceWrapper = this.createBeanInstance(beanName, mbd, args);
        }
        final Object bean = instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null;
        Class beanType = instanceWrapper != null ? instanceWrapper.getWrappedClass() : null;
        Object var7 = mbd.postProcessingLock;
        synchronized(mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
                //這裏的處理方式和c.2的處理方式相似
                this.applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                mbd.postProcessed = true;
            }
        }
        boolean earlySingletonExposure = mbd.isSingleton() && this.allowCircularReferences && this.isSingletonCurrentlyInCreation(beanName);
        if (earlySingletonExposure) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references");
            }
            this.addSingletonFactory(beanName, new ObjectFactory() {
                public Object getObject() throws BeansException {
                    return AbstractAutowireCapableBeanFactory.this.getEarlyBeanReference(beanName, mbd, bean);
                }
            });
        }
        Object exposedObject = bean;
        try {
            this.populateBean(beanName, mbd, instanceWrapper);
            if (exposedObject != null) {
                //這裏就是初始化的方法了
                exposedObject = this.initializeBean(beanName, exposedObject, mbd);
            }
        } catch (Throwable var17) {
            if (var17 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var17).getBeanName())) {
                throw (BeanCreationException)var17;
            }
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var17);
        }
        if (earlySingletonExposure) {
            Object earlySingletonReference = this.getSingleton(beanName, false);
            if (earlySingletonReference != null) {
                if (exposedObject == bean) {
                    exposedObject = earlySingletonReference;
                } else if (!this.allowRawInjectionDespiteWrapping && this.hasDependentBean(beanName)) {
                    String[] dependentBeans = this.getDependentBeans(beanName);
                    Set<String> actualDependentBeans = new LinkedHashSet(dependentBeans.length);
                    String[] var15 = dependentBeans;
                    int var14 = dependentBeans.length;
                    for(int var13 = 0; var13 < var14; ++var13) {
                        String dependentBean = var15[var13];
                        if (!this.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.");
                    }
                }
            }
        }
        try {
            this.registerDisposableBeanIfNecessary(beanName, bean, mbd);
            return exposedObject;
        } catch (BeanDefinitionValidationException var16) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", var16);
        }
    }
   protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
        if (System.getSecurityManager() != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
                    return null;
                }
            }, this.getAccessControlContext());
        } else {
            this.invokeAwareMethods(beanName, bean);
        }
        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
            //執行BeanPostProcessors的postProcessBeforeInitialization方法
            wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
        }
        try {
            //執行實現InitializingBean,和init-method標籤的內容
            this.invokeInitMethods(beanName, wrappedBean, mbd);
        } catch (Throwable var6) {
            throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
        }
        if (mbd == null || !mbd.isSynthetic()) {
            //執行BeanPostProcessors的postProcessAfterInitialization方法
            wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }
        return wrappedBean;
    }
   public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
        Object result = existingBean;
        Iterator var5 = this.getBeanPostProcessors().iterator();
        while(var5.hasNext()) {
            BeanPostProcessor beanProcessor = (BeanPostProcessor)var5.next();
            result = beanProcessor.postProcessBeforeInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
        Object result = existingBean;
        Iterator var5 = this.getBeanPostProcessors().iterator();
        while(var5.hasNext()) {
            BeanPostProcessor beanProcessor = (BeanPostProcessor)var5.next();
            result = beanProcessor.postProcessAfterInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }
  protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
        boolean isInitializingBean = bean instanceof InitializingBean;
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
            }
            if (System.getSecurityManager() != null) {
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                            ((InitializingBean)bean).afterPropertiesSet();
                            return null;
                        }
                    }, this.getAccessControlContext());
                } catch (PrivilegedActionException var6) {
                    throw var6.getException();
                }
            } else {
                //調用InitializingBean接口的afterPropertiesSet方法,初始化
                ((InitializingBean)bean).afterPropertiesSet();
            }
        }
        if (mbd != null) {
            String initMethodName = mbd.getInitMethodName();
            if (initMethodName != null && (!isInitializingBean || !"afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) {
                //執行init-method的方法,若是afterPropertiesSet執行出現異常,是不會執行init-method的方法的
                this.invokeCustomInitMethod(beanName, bean, mbd);
            }
        }
    }

    protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
        String initMethodName = mbd.getInitMethodName();
        final Method initMethod = mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName, new Class[0]) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName, new Class[0]);
        if (initMethod == null) {
            if (mbd.isEnforceInitMethod()) {
                throw new BeanDefinitionValidationException("Couldn't find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'");
            } else {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("No default init method named '" + initMethodName + "' found on bean with name '" + beanName + "'");
                }
            }
        } else {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Invoking init method  '" + initMethodName + "' on bean with name '" + beanName + "'");
            }
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ReflectionUtils.makeAccessible(initMethod);
                        return null;
                    }
                });
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                            initMethod.invoke(bean);
                            return null;
                        }
                    }, this.getAccessControlContext());
                } catch (PrivilegedActionException var9) {
                    InvocationTargetException ex = (InvocationTargetException)var9.getException();
                    throw ex.getTargetException();
                }
            } else {
                try {
                    ReflectionUtils.makeAccessible(initMethod);
                    //執行方法
                    initMethod.invoke(bean);
                } catch (InvocationTargetException var8) {
                    throw var8.getTargetException();
                }
            }

        }
    }

  d.4 建立bean的過程結束了,這裏主要的目的是初始化。

  4、bean的加載基本上就是這樣了,不過仍是梳理一下比較好

  1)經過getBean的方式進行,進行bean的獲取

  2)bean的獲取現從singletonObjects中獲取,若是沒有在進行createBean

  3)bean的獲取是從beanDefinitionMap裏面開始的,獲取到對應的BeanDefinition,而後在進行處理

  4)createBean的過程當中是實例化了bean並放入BeanDefinition的,而後進行了各類初始化

  5)在調用createBean的結束的時候在將bean放入了singletonObjects單例裏面,這個實在源碼解析c中描述的

  6)將得到bean進行返回,若是存在class類型,則適配返回,沒有就object

  5、bean的加載過程基本上流程就這樣,固然源碼的解析過程,是比較痛苦的,可是在實現他的每個實現都有他的意義。我這裏只是更明顯的闡述,實際的調用過程。固然這只是開始,後續的東西都是以此基礎來進行拓展的。特別是在自定義標籤環節。基本上拓展的新功能,都是在自定義標籤下面實現的。

  6、bean的基本過程就是這樣子了,而後就是後續的拓展功能,bean的加強化,仍是自定義標籤的使用上面,後續都會一一呈現,目的是更好的理解spring的拓展性和易用性

相關文章
相關標籤/搜索