spring源码-bean之加载-2

  一、前面说了bean的容器初始化,后面当然是说bean的加载。这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉通过BeanFactory是怎么实现class的实例化的。

  二、声明一下:XmlBeanFactory和ApplicationContext的不同在与,ApplicationContext在容器初始化的时候,就做了很多准备操作。而XmlBeanFactory是在获取bean的时候才会调用初始化的东西。这一点是XmlBeanFactory做的不好的地方。比如:BeanPostProcessor、InitializingBean和标签init-method等,这些都是在bean加载的时候完成而不是在初始化的时候。

  三、bean的加载源码解读

  1)bean的获取方式

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)getBean

   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、createBean

  a、getSingleton

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、getMergedLocalBeanDefinition

   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的地方)

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、createBean

    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的过程结束了,这里主要的目的是初始化。

  四、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

  五、bean的加载过程基本上流程就这样,当然源码的解析过程,是比较痛苦的,但是在实现他的每一个实现都有他的意义。我这里只是更明显的阐述,实际的调用过程。当然这只是开始,后续的东西都是以此基础来进行拓展的。特别是在自定义标签环节。基本上拓展的新功能,都是在自定义标签下面实现的。

  六、bean的基本过程就是这样子了,然后就是后续的拓展功能,bean的增强化,还是自定义标签的使用上面,后续都会一一呈现,目的是更好的理解spring的拓展性和易用性

原文地址:https://www.cnblogs.com/ll409546297/p/9482669.html

时间: 2024-10-09 00:42:27

spring源码-bean之加载-2的相关文章

Spring源码分析之-加载IOC容器

本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spring bean容器的刷新的核心 11个步骤进行祭拜(一定要让我学会了...阿门) // 完成IoC容器的创建及初始化工作 @Override public void refresh() throws BeansException, IllegalStateException { synchroni

MyBatis 源码篇-资源加载

本章主要描述 MyBatis 资源加载模块中的 ClassLoaderWrapper 类和 Java 加载配置文件的三种方式. ClassLoaderWrapper 上一章的案例,使用 org.apache.ibatis.io.Resources#getResourceAsStream(java.lang.String) 方法加载 MyBatis 的配置文件.Resources 是一个提供了多个静态方法的工具类,内部封装了 ClassLoaderWrapper 类的静态字段,Resources 

Prism 源码解读3-Modules加载

原文:Prism 源码解读3-Modules加载 目录 介绍 0.Modules加载 1.通过AppSetting加载 2.通过代码加载 3.通过目录加载 4.通过手动方式加载 总结 回到顶部 介绍 在软件开发过程中,总想组件式的开发方式,各个组件之间最好互不影响,独立测试.Prism的Modules很好的满足了这一点. 这个架构图很好了讲解了Prism的Modules的概念 Prism支持通过配置文件,文件夹,手动载入Module的方式,并且对Module的载入进行验证,包括重复和循环依赖验证

spring源码-bean之初始化-1

一.spring的IOC控制反转:控制反转--Spring通过一种称作控制反转(IOC)的技术促进了松耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.说白了就是原本需要手动去创建和管理的对象,现在交给容器了.在容器的基础上,实现对象的控制.这个方式也很好的减少了开发者在框架上面的时间,更多关注点在业务逻辑上面. 二.这表博客的主要目的是研究,bean容器的初始化,会在最基本的bean初始化作源码的解析和分析.如何存在不妥当的地方,还请

Spring源码--Bean的管理总结(一)

前奏 最近看了一系列解析spring管理Bean的源码的文章,在这里总结下,方便日后复盘.文章地址https://www.cnblogs.com/CodeBear/p/10336704.html spring的一大核心就是Ioc,即把Bean放到一个公共的容器中,既替开发者管理了Bean的生命周期,又解耦了类之间的持有关系. spring中,管理Bean的容器都叫xxxContext,这里的继承关系有点复杂,但是功能是相同的--存放并管理各种Bean.我们常用的用注解生成的Bean,都放在Ann

JQuery源码解析-Dom加载过程

下面的几个工具方法都是和dom加载有关,所以先从dom加载开始. 用到的方法: isReady:DOM是否已经加载完(内部使用) readyWait():等待多少文件的计时器(内部使用) holdReady()::推迟DOM触发 ready():准备DOM触发 jQuery.ready.promise = function( obj ) {}检测dom的异步操作 先看一下jQuery和原生js加载方式有什么不同: $(function () { }); window.onload = funct

Android Launcher源码研究(二) 加载app流程1

今天主要分析Android Launcher源码中的一些重要类之间的关系,基本的加载流程.先来看一个类图 Launcher.java 是主Activity 在onCreate方法里面初始化了LauncherMode实例. LauncherApplication app = ((LauncherApplication)getApplication()); mModel = app.setLauncher(this); 直接进入LauncherApplication.java的方法 Launcher

ListActivity和ListView源码分析(1) 加载布局

最近写代码总是用到ListActivity,因此看了下ListView和ListActivity的源代码. 1 加载布局 一般来说我们使用自定义的ListActivity时,在创建时首先需要调用setContentView函数来设置这个Activity对应的view布局. 而从Activity.onContentChanged()的api文档说明来看,setContentView方法会导致onContentChanged方法被调用.因此我们接下来来看下ListActivity的onContent

Android Launcher源码研究(三) 加载app流程2

接上次的. 首先Launcher实现了LauncherModel.Callbacks接口,APP信息数据加载成功后 ,回调接口把app信息显示到Launcher的 workspace界面上,这个过程代码里面称为bind. 下面是个类调用过程的时序图,不是很标准,不过能表达基本调用顺序帮助我们理解. 首先就是Launcher OnCreate中调用LauncherMode startLoader方法,这里只看异步的方式 就是当前的页面下标为-1,加载所有app信息 mWorkspace.getCu