spring IOC之篇五 bean的加载

我们终于结束了对XML配置文件的解析,接下来我们要分析bean的加载,即对代码getBean的分析:

People p = (People)ctx.getBean("cutesource");

@SuppressWarnings("unchecked")
    protected <T> T doGetBean(
            final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
            throws BeansException {
 // 根据名称获取对应的beanName。如果传入的别名A指向bean B则返回bean B的beanName,如果别名A指向别名B 然后别名B指向bean c 则返回bean C,如果传入的是FactoryBean,则则去掉修饰符 ,比如 name=”&aa” 则变为 name=”aa”
        final String beanName = transformedBeanName(name);
        Object bean;

        // Eagerly check singleton cache for manually registered singletons.
       // 尝试从缓存中加载单例。首次从单例缓存中加载,如果不成功则从singletonFactories中加载。因为在创建单例的时候存在着循环依赖,所以不等bean创建完成就就将创建bean的ObjectFactory提早曝光加入到缓存中,一旦下一个bean依赖上一个bean 则直接使用ObjectFactory
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            if (logger.isDebugEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.debug("Returning eagerly cached instance of singleton bean ‘" + beanName +
                            "‘ that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.debug("Returning cached instance of singleton bean ‘" + beanName + "‘");
                }
            }
 // 返回对应的实例,缓存中记录的只是bean最初始化的状态,不一定是最终的bean,有时间需要返回时的指定方法实例化的实例,例如 FactoryBean的getObject的方法的实例
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else {
            // Fail if we‘re already creating this bean instance:
            // We‘re assumably within a circular reference.
       // 只有在单例的情况下面才去解决循环依赖,如果原型的情况下面则直接抛出异常
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }

            // Check if bean definition exists in this factory.

            BeanFactory parentBeanFactory = getParentBeanFactory();
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                // Not found -> check parent.
               // 如果parentBeanFactory 不为空,且当前的beanFactory不包含beanName则递归到他的上一层去加载beanName
                String nameToLookup = originalBeanName(name);
                if (args != null) {
                    // Delegation to parent with explicit args.
                // args为传入的构造函数的参数
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else {
                     // 没有构造参数则使用标准的方法获取bean
                    // No args -> delegate to standard getBean method.
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
            }

            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }

            try {
               // 将GenericBeanDefinition转换为 RootBeanDefinition
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
               // 如果父类bean不为空的话,合并一下父类的属性
                checkMergedBeanDefinition(mbd, beanName, args);

                // Guarantee initialization of beans that the current bean depends on.
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
               // 获取bean的其他依赖,如果依赖其他的bean,则先获取其他的bean
                    for (String dependsOnBean : dependsOn) {
                        if (isDependent(beanName, dependsOnBean)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between ‘" + beanName + "‘ and ‘" + dependsOnBean + "‘");
                        }
            // 把该bean依赖的其他的bean加入到dependentBeanMap集合中去
                        registerDependentBean(dependsOnBean, beanName);
  // 获取其依赖的其他bean
                        getBean(dependsOnBean);
                    }
                }

                // Create bean instance.
     // 根据不同的scope进行初始化策略
                if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
                        @Override
                        public Object getObject() throws BeansException {
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            catch (BeansException ex) {
                                // Explicitly remove instance from singleton cache: It might have been put there
                                // eagerly by the creation process, to allow for circular reference resolution.
                                // Also remove any beans that received a temporary reference to the bean.
                                destroySingleton(beanName);
                                throw ex;
                            }
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

                else if (mbd.isPrototype()) {
                    // It‘s a prototype -> create a new instance.
                    Object prototypeInstance = null;
                    try {
                        beforePrototypeCreation(beanName);
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {
                        afterPrototypeCreation(beanName);
                    }
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {
                    String scopeName = mbd.getScope();
                    final Scope scope = this.scopes.get(scopeName);
                    if (scope == null) {
                        throw new IllegalStateException("No Scope registered for scope name ‘" + scopeName + "‘");
                    }
                    try {
                        Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
                            @Override
                            public Object getObject() throws BeansException {
                                beforePrototypeCreation(beanName);
                                try {
                                    return createBean(beanName, mbd, args);
                                }
                                finally {
                                    afterPrototypeCreation(beanName);
                                }
                            }
                        });
                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    }
                    catch (IllegalStateException ex) {
                        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",
                                ex);
                    }
                }
            }
            catch (BeansException ex) {
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }
// 根据传入的参数 requiredType和返回的bean类型不一致的时候,进行装换
        // Check if required type matches the type of the actual bean instance.
        if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
            try {
                return getTypeConverter().convertIfNecessary(bean, requiredType);
            }
            catch (TypeMismatchException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to convert bean ‘" + name + "‘ to required type [" +
                            ClassUtils.getQualifiedName(requiredType) + "]", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }

进一步分析 FactoryBean的使用

FactoryBean接口对于Spring框架来说占有重要的地位,Spring自身就提供了70多种实现类。他们隐藏了一下实例化的复杂的细节,给上层应用带来便利。

public interface FactoryBean<T> {
   // 返回由FactoryBean创建的Bean实例,如果isSingleton()方法返回的是true,则该实例会放入到Spring容器的单例缓存池当中
    T getObject() throws Exception;

    // 返回FactoryBean创建的bean类型
    Class<?> getObjectType();
   // 返回由factoryBean创建实例的Bean是singleton还是prototype
    boolean isSingleton();

}

创建一个实体用Factory进行实例化

public class People {
        private String id;
        private String name;
        private Integer age;
        public String getId() {
            return id;
        }

创建一个自定义的FactoryBean方法

public class PeopleFactoryBean implements FactoryBean<People> {

    private String peopleInfo;

    public People getObject() throws Exception {

        People people = new People();
        String[] infos = peopleInfo.split(",");
        people.setId(infos[0]);
        people.setName(infos[1]);
        people.setAge(Integer.parseInt(infos[2]));
        return people;
    }

    public Class<People> getObjectType() {
        return People.class;
    }

    public boolean isSingleton() {
        return false;
    }

    public String getPeopleInfo() {
        return peopleInfo;
    }

    public void setPeopleInfo(String peopleInfo) {
        this.peopleInfo = peopleInfo;
    }

}

配置文件中配置:

<bean id="people" class="com.spring.test.factory.PeopleFactoryBean">
        <property name="peopleInfo" value="gh2,address2,22"></property>
</bean> 

即可使用自定义的逻辑进行实例化,当调用getBean的时候,Spring通过反射机制发现PeopleFactoryBean实现了FactoryBean,这时候Spring容器就调用接口方法PeopleFactoryBean#getObject()方法返回。如果希望获取PeopleFactoryBean实例,则在beanName前面显示的加&

原文地址:https://www.cnblogs.com/histlyb/p/8988808.html

时间: 2024-10-08 06:05:36

spring IOC之篇五 bean的加载的相关文章

spring IOC之篇六 bean的加载---bean的创建

之前我们讲解了缓存中单例的获取,解下来需要讲解缓存中不存在该如何处理 public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(beanName, "'beanName' must not be null"); // 全局变量需要同步 synchronized (this.singletonObjects) { // 先从单例缓存池获取,以便

Spring源码学习(五)bean的加载

加油加油 ?? bean加载的大致过程 1 /** 2 * Return an instance, which may be shared or independent, of the specified bean. 3 * 4 * @param name the name of the bean to retrieve 5 * @param requiredType the required type of the bean to retrieve 6 * @param args argume

spring源码阅读之Bean的加载(二)

在正式分析源码之前,先来了解一下SpringBeans里面最核心的两个类  DefaultListableBeanFactory XMLBean继承自 DefaultListableBeanFactory,而 DefaultListableBeanFactory是整个Bean加载的核心部分,是Sprin注册及加载Bean的默认实现,而对于XmlBeanFactory与 DefaultListableBeanFactory不同的地方其实就是在XmlBeanFactory中使用了自定义的XML读取器

spring源码阅读之Bean的加载(一)

Bean的概念 Bean应该是Spring里面最核心的东西了,我觉得Bean这个名字起的还是很形象的,Bean 豆:豆形种子 Spring应该就是包括在豌豆最外层的那层衣服了,而Bean就是里面的一颗一颗的豆子.我们平常开发中就是把Spring里面塞入一颗一颗的豆子.根据面向对象的开发原则,Bean其实也就是个对象,来看一看我们一个正常的系统中都配置了什么Bean: 按照面向对象的说法,我们在容器里面放入了如图所示的对象,当然这个对象可以是一个类,一个文件,一个管理器,一个接口,甚至是一个Jav

spring源码深度解析— IOC 之 开启 bean 的加载

概述 前面我们已经分析了spring对于xml配置文件的解析,将分析的信息组装成 BeanDefinition,并将其保存注册到相应的 BeanDefinitionRegistry 中.至此,Spring IOC 的初始化工作完成.接下来我们将对bean的加载进行探索. 之前系列文章: spring源码深度解析— IOC 之 容器的基本实现 spring源码深度解析— IOC 之 默认标签解析(上) spring源码深度解析— IOC 之 默认标签解析(下) spring源码深度解析— IOC

【Spring源码分析系列】bean的加载

前言 以 BeanFactory bf  = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过程. 一.首先来看Spring中是如何实现的 1 @Override 2 public Object getBean(String name) throws BeansException { 3 return getBean(name, Object.class); 4 } 5 6 @Override 7 p

【Spring】详解Spring中Bean的加载

之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,从之前的例子开始,Spring中加载一个bean的方式: TestBean bean = factory.getBean("testBean"); 来看看getBean(String name)方法源码, @Override public Object getBean(String name) throws BeansException { re

spring源码-bean之加载-2

一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉通过BeanFactory是怎么实现class的实例化的. 二.声明一下:XmlBeanFactory和ApplicationContext的不同在与,ApplicationContext在容器初始化的时候,就做了很多准备操作.而XmlBeanFactory是在获取bean的时候才会调用初始化的东西

【Spring源码分析】非懒加载的Bean实例化过程(下篇)

doCreateBean方法 上文[Spring源码分析]非懒加载的Bean实例化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireCapableBeanFactory的doCreateBean方法代码: 1 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[]