Spring Bean研究

这篇博客详细探讨了Spring框架中Bean的创建与注册流程。从ApplicationContext的初始化开始,解析了如何通过ClassPathXmlApplicationContext构造函数调用refresh()方法来加载和装配Bean。在创建beanFactory时,如果已有beanFactory则会销毁并关闭。然后,通过AbstractXmlApplicationContext的loadBeanDefinitions()方法,使用XmlBeanDefinitionReader解析XML配置文件,加载Bean定义。最后,BeanDefinition被注册到DefaultListableBeanFactory中,确保线程安全,并可能注册别名。此外,还介绍了在获取Bean时,如何从单例缓存中查找或创建Bean实例。
摘要由CSDN通过智能技术生成

1.Spring IOC容器装载及注册bean的过程

ApplicationContext context = newClassPathXmlApplicationContext(

               new String[] {"applicationContext-aop1.xml"});

其中ClassPathXmlApplicationContext继承抽象类AbstractApplicationContext

上面那行代码的通过调用ClassPathXmlApplicationContext的构造函数,调用AbstractApplicationContext的refresh()方法,实现IOC容器读取配置源数据,实例化,配置和组装bean。

由于Spring,每实例化一次ClassPathXmlApplicationContext,就会对应于一个beanFactory,也就是说一个IOC容器对应一个beanFactory,所以在IOC容器读取配置源数据,实例化,配置和组装bean前,要刷新beanFactory,并获取刷新后的beanFactory。

抽象类AbstractRefreshableApplicationContext继承抽象类AbstractApplicationContext,并实现AbstractApplicationContextrefreshBeanFactory()刷新beanFactory.

/**

     * This implementation performs an actual refresh of this context's underlying

     * bean factory, shutting down the previous bean factory (if any) and

     * initializing a fresh bean factory for the next phase of the context's lifecycle.

     */

    @Override

    protectedfinalvoid refreshBeanFactory() throws BeansException {

      if (hasBeanFactory()) {

           destroyBeans();

           closeBeanFactory();

       }

       try {

             //创建一个DefaultListableBeanFactory的beanFactory

           DefaultListableBeanFactory beanFactory = createBeanFactory();

           beanFactory.setSerializationId(getId());

           customizeBeanFactory(beanFactory);

             //这段代码是真正意义上的通过创建的beanFactory,读取源配置文件以及装载bean。

           loadBeanDefinitions(beanFactory);

           synchronized (this.beanFactoryMonitor) {

              this.beanFactory = beanFactory;

           }

       }

       catch (IOException ex) {

           thrownew ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);

       }

    }

从上面的代码可以看出,在创建beanFactory的时候,要判断是否已经存在beanFactory,如果存在就销毁,并关闭beanFactory。

  抽象类AbstractXmlApplicationContext继承抽象类AbstractRefreshableApplicationContext,并实现loadBeanDefinitions()抽象方法。

/**

     * Loads the bean definitions via an XmlBeanDefinitionReader.

     * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader

     * @see #initBeanDefinitionReader

     * @see #loadBeanDefinitions

     */

    @Override

    protectedvoid loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {

       // Create a new XmlBeanDefinitionReader for the given BeanFactory.

      XmlBeanDefinitionReader beanDefinitionReader =new XmlBeanDefinitionReader(beanFactory);

 

       // Configure the bean definition reader with this context's

       // resource loading environment.

       beanDefinitionReader.setEnvironment(this.getEnvironment());

       beanDefinitionReader.setResourceLoader(this);

       beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

 

       // Allow a subclass to provide custom initialization of the reader,

       // then proceed with actually loading the bean definitions.

       initBeanDefinitionReader(beanDefinitionReader);

        //解析并装载bean

       loadBeanDefinitions(beanDefinitionReader);

    }

从loadBeanDefinitions()方法可以看出,Spring主要是通过XmlBeanDefinitionReader来解析源配置文件。

/**

     * Load the bean definitions with the given XmlBeanDefinitionReader.

     * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}

     * method; hence this method is just supposed to load and/or register bean definitions.

     * @param reader the XmlBeanDefinitionReader to use

     * @throws BeansException in case of bean registration errors

     * @throws IOException if the required XML document isn't found

     * @see #refreshBeanFactory

     * @see #getConfigLocations

     * @see #getResources

     * @see #getResourcePatternResolver

     */

    protectedvoidloadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {

      Resource[] configResources = getConfigResources();

       if (configResources != null) {

           reader.loadBeanDefinitions(configResources);

       }

       String[] configLocations = getConfigLocations();

       if (configLocations != null) {

           reader.loadBeanDefinitions(configLocations);

       }

    }

调用XmlBeanDefinitionReader的子类AbstractBeanDefinitionReaderloadBeanDefinitions()方法

/**

     * Load bean definitions from the specified resource location.

     * <p>The location can also be a location pattern, provided that the

     * ResourceLoader of this bean definition reader is a ResourcePatternResolver.

     * @param location the resource location, to be loaded with the ResourceLoader

     * (or ResourcePatternResolver) of this bean definition reader

     * @param actualResources a Set to be filled with the actual Resource objects

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

架构随笔录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值