spring装配Bean过程以及Bean实例化过程

简要说明Spring什么时候实例化bean,首先要分2种情况
  第一:如果你使用BeanFactory作为Spring Bean的工厂类,则所有的bean都是在第一次使用该Bean的时候实例化
  第二:如果你使用ApplicationContext作为Spring Bean的工厂类,则又分为以下几种情况:
       (1):如果bean的scope是singleton的,并且lazy-init为false(默认是false,所以可以不用设置),则 ApplicationContext启动的时候就实例化该Bean,并且将实例化的Bean放在一个map结构的缓存中,下次再使用该Bean的时候, 直接从这个缓存中取
       (2):如果bean的scope是singleton的,并且lazy-init为true,则该Bean的实例化是在第一次使用该Bean的时候进行实例化
       (3):如果bean的scope是prototype的,则该Bean的实例化是在第一次使用该Bean的时候进行实例化

Spring中Bean的实例化过程
http://static.oschina.net/uploads/space/2011/1222/204039_9370_218421.jpg
打印Spring初始化bean的时间

  1. @Service
  1. publicclassBeanPostProcessorImplimplementsBeanPostProcessor{
  1. privatestaticfinalThreadLocal<Long> threadLocal =newThreadLocal<Long>();
  1. @Override
  1. publicObject postProcessBeforeInitialization(Object o,String s)throwsBeansException{
  1. long start =System.currentTimeMillis();
  1. threadLocal.set(start);
  1. return o;
  1. }
  1. @Override
  1. publicObject postProcessAfterInitialization(Object o,String s)throwsBeansException{
  1. System.out.println(o.getClass().getName()+", "+ s+" init time : "+(System.currentTimeMillis()- threadLocal.get()));
  1. return o;
  1. }
  1. }

获取所有bean refresh完事件通知

实现如下接口ApplicationListener<ContextRefreshedEvent>


Spring 对同一个文件import多次
<ol class="linenums" style="margin:0px; word-wrap:break-word; padding:0px 0px 0px 40px"><li class="L0" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">applicationContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">(</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">parent</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">)</span></span></code></li><li class="L1" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">         </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">aContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span></span></code></li><li class="L2" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">             </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">bContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">(初始化)</span></span></code></li><li class="L3" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">             </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">cContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">(初始化)</span></span></code></li><li class="L4" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">         </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">bContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">(初始化,覆盖原来的</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">bean</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">)</span></span></code></li><li class="L5" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">         </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">cContext</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">xml</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">(初始化,覆盖原来的</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">bean</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">)</span></span></code></li></ol>

Spring源码阅读笔记

注册bean的定义代码:

<ol class="linenums" style="margin:0px; word-wrap:break-word; padding:0px 0px 0px 40px"><li class="L0" style="word-wrap:break-word; padding-left:0px; color:rgb(190,190,197)"><code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; padding:0px; color:inherit; word-wrap:break-word; background-color:transparent"><span style="font-size:18px"><span class="typ" style="word-wrap:break-word; color:rgb(0,128,128)">AbstractBeanDefinitionReader</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">loadBeanDefinitions </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">-->..</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)"> </span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">--></span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)"> </span><span class="typ" style="word-wrap:break-word; color:rgb(0,128,128)">DefaultListableBeanFactory</span><span class="pun" style="word-wrap:break-word; color:rgb(147,161,161)">.</span><span class="pln" style="word-wrap:break-word; color:rgb(72,72,76)">registerBeanDefinition</span></span></code></li></ol>
  • 如果重复的bean name定义在同一个文件中,那么编译错误
  • 如果重复的bean name定义在不同的文件中,类型可以不一样,那么后加载的那个bean会将先加载的bean覆盖掉。(类型不一样时,可能导致其他类的引用出错)——weibo rpc的配置中,如果两个文件引用同一个refer,那么会报错nested exception is java.lang.IllegalStateException: Duplicate spring bean id
  • 如果多次import同一个文件,那么相当于在不同的文件中定义了相同的bean。
  • 如果allowBeanDefinitionOverriding被设置为false,那么不能有相同名字的bean。

  • 使用BeanPostProcessor打印bean的加载时间,顺序可能跟bean的初始化顺序不一致。
  • AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml"); //随着spring容器加载,就不会实例化bean。

    Person person = ctx.getBean("person");//这一步才在实例化bean。就是前面说的需要的时候再实例化了。

    下面就是实例化bean的过程。

    实例化bean测试结果:先构造函数——>然后是b的set方法注入—— >InitializingBean 的afterPropertiesSet方法——>init- method方法

    教科书上总结为:
    一、Spring装配Bean的过程

    1. 实例化;
    2. 设置属性值;
    3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
    4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
    5.如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
    6. 调用BeanPostProcessor的预先初始化方法;
    7. 调用InitializingBean的afterPropertiesSet()方法;
    8. 调用定制init-method方法;
    9. 调用BeanPostProcessor的后初始化方法;


    Spring容器关闭过程
    1. 调用DisposableBean的destroy();
    2. 调用定制的destroy-method方法;


阅读更多
换一批

没有更多推荐了,返回首页