spring源码-bean之加载-2

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

  一、前面说了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的拓展性和易用性

相关文章
|
8天前
|
数据采集 监控 前端开发
二级公立医院绩效考核系统源码,B/S架构,前后端分别基于Spring Boot和Avue框架
医院绩效管理系统通过与HIS系统的无缝对接,实现数据网络化采集、评价结果透明化管理及奖金分配自动化生成。系统涵盖科室和个人绩效考核、医疗质量考核、数据采集、绩效工资核算、收支核算、工作量统计、单项奖惩等功能,提升绩效评估的全面性、准确性和公正性。技术栈采用B/S架构,前后端分别基于Spring Boot和Avue框架。
|
27天前
|
XML Java 数据格式
Spring从入门到入土(bean的一些子标签及注解的使用)
本文详细介绍了Spring框架中Bean的创建和使用,包括使用XML配置文件中的标签和注解来创建和管理Bean,以及如何通过构造器、Setter方法和属性注入来配置Bean。
58 9
Spring从入门到入土(bean的一些子标签及注解的使用)
|
17天前
|
Java 测试技术 Windows
咦!Spring容器里为什么没有我需要的Bean?
【10月更文挑战第11天】项目经理给小菜分配了一个紧急需求,小菜迅速搭建了一个SpringBoot项目并完成了开发。然而,启动测试时发现接口404,原因是控制器包不在默认扫描路径下。通过配置`@ComponentScan`的`basePackages`字段,解决了问题。总结:`@SpringBootApplication`默认只扫描当前包下的组件,需要扫描其他包时需配置`@ComponentScan`。
|
23天前
|
Java Spring
Spring底层架构源码解析(三)
Spring底层架构源码解析(三)
|
23天前
|
XML Java 数据格式
Spring底层架构源码解析(二)
Spring底层架构源码解析(二)
|
27天前
|
Java 开发者 Spring
Spring bean的生命周期详解!
本文详细解析Spring Bean的生命周期及其核心概念,并深入源码分析。Spring Bean是Spring框架的核心,由容器管理其生命周期。从实例化到销毁,共经历十个阶段,包括属性赋值、接口回调、初始化及销毁等。通过剖析`BeanFactory`、`ApplicationContext`等关键接口与类,帮助你深入了解Spring Bean的管理机制。希望本文能助你更好地掌握Spring Bean生命周期。
61 1
|
29天前
|
Java Spring
获取spring工厂中bean对象的两种方式
获取spring工厂中bean对象的两种方式
23 1
|
30天前
|
设计模式 JavaScript Java
Spring 事件监听机制源码
Spring 提供了事件发布订阅机制,广泛应用于项目中。本文介绍了如何通过自定义事件类、订阅类和发布类实现这一机制,并展示了如何监听 SpringBoot 启动过程中的多个事件(如 `ApplicationStartingEvent`、`ApplicationEnvironmentPreparedEvent` 等)。通过掌握这些事件,可以更好地理解 SpringBoot 的启动流程。示例代码展示了从事件发布到接收的完整过程。
|
24天前
|
XML Java 数据格式
手动开发-简单的Spring基于注解配置的程序--源码解析
手动开发-简单的Spring基于注解配置的程序--源码解析
38 0
|
24天前
|
XML Java 数据格式
手动开发-简单的Spring基于XML配置的程序--源码解析
手动开发-简单的Spring基于XML配置的程序--源码解析
71 0