少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          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的時候進行實例化 


          1、lazy init 在getBean時實例化 
          2、非lazy的單例bean 容器初始化時實例化 
          3、prototype等 getBean時實例化




          spring三種實例化bean的方式

          在spring中有三中實例化bean的方式:

          一、使用構造器實例化;

          二、使用靜態工廠方法實例化;

          三、使用實例化工廠方法實例化。

           

          每種實例化所采用的配置是不一樣的:

          一、使用構造器實例化;

          這種實例化的方式可能在我們平時的開發中用到的是最多的,因為在xml文件中配置簡單并且也不需要額外的工廠類來實現。

          <!--applicationContext.xml配置:-->

          <bean id="personService" class="cn.mytest.service.impl.PersonServiceBean"></bean>
           id是對象的名稱,class是要實例化的類,然后再通過正常的方式進調用實例化的類即可,比如:
          public void instanceSpring(){
                          //加載spring配置文件
          ApplicationContext ac = new ClassPathXmlApplicationContext(
          new String[]{
          "/conf/applicationContext.xml"
          });
          //調用getBean方法取得被實例化的對象。
          PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService");
          psb.save();
          }

          采用這種實例化方式要注意的是:要實例化的類中如果有構造器的話,一定要有一個無參的構造器。

           

          二、使用靜態工廠方法實例化;

          根據這個中實例化方法的名稱就可以知道要想通過這種方式進行實例化就要具備兩個條件:(一)、要有工廠類及其工廠方法;(二)、工廠方法是靜態的。OK,知道這兩點就好辦了,首先創建工程類及其靜態方法:

          package cn.mytest.service.impl;
          /**
          *創建工廠類
          *
          */
          public class PersonServiceFactory {
              //創建靜態方法
          public static PersonServiceBean createPersonServiceBean(){
                   //返回實例化的類的對象
          return new PersonServiceBean();
          }
          }
          然后再去配置spring配置文件,配置的方法和上面有點不同,這里也是關鍵所在
          <!--applicationContext.xml配置:-->
          <bean id="personService1" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"></bean>

           id是實例化的對象的名稱,class是工廠類,也就實現實例化類的靜態方法所屬的類,factory-method是實現實例化類的靜態方法。

          然后按照正常的調用方法去調用即可:

          public void instanceSpring(){
                          //加載spring配置文件
          ApplicationContext ac = new ClassPathXmlApplicationContext(
          new String[]{
          "/conf/applicationContext.xml"
          });
          //調用getBean方法取得被實例化的對象。
          PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService1");
          psb.save();
          }

          三、使用實例化工廠方法實例化。

          這個方法和上面的方法不同之處在與使用該實例化方式工廠方法不需要是靜態的,但是在spring的配置文件中需要配置更多的內容,,首先創建工廠類及工廠方法:

          package cn.mytest.service.impl;
          /**
          *創建工廠類
          *
          */
          public class PersonServiceFactory {
              //創建靜態方法
          public PersonServiceBean createPersonServiceBean1(){
                   //返回實例化的類的對象
          return new PersonServiceBean();
          }
          }
          然后再去配置spring配置文件,配置的方法和上面有點不同,這里也是關鍵所在
          <!--applicationContext.xml配置:-->
          <bean id="personServiceFactory" class="cn.mytest.service.impl.PersonServiceFactory"></bean>
          <bean id="personService2" factory-bean="personServiceFactory" factory-method="createPersonServiceBean1"></bean>

           這里需要配置兩個bean,第一個bean使用的構造器方法實例化工廠類,第二個bean中的id是實例化對象的名稱,factory-bean對應的被實例化的工廠類的對象名稱,也就是第一個bean的id,factory-method是非靜態工廠方法。

           

           

          然后按照正常的調用方法去調用即可:

          public void instanceSpring(){
                          //加載spring配置文件
          ApplicationContext ac = new ClassPathXmlApplicationContext(
          new String[]{
          "/conf/applicationContext.xml"
          });
          //調用getBean方法取得被實例化的對象。
          PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService2");
          psb.save();
          }
          posted on 2015-04-20 15:01 abin 閱讀(478) 評論(0)  編輯  收藏 所屬分類: spring
          主站蜘蛛池模板: 太白县| 轮台县| 铁力市| 会理县| 化州市| 开封县| 同心县| 当涂县| 鄂托克前旗| 安陆市| 田东县| 堆龙德庆县| 六安市| 咸宁市| 和平县| 秦皇岛市| 同江市| 家居| 合阳县| 乐都县| 昆明市| 鹤山市| 河南省| 辽中县| 兰坪| 柳江县| 翁牛特旗| 陇南市| 博罗县| 塔城市| 东阿县| 泰来县| 什邡市| 保亭| 尤溪县| 噶尔县| 历史| 余江县| 汨罗市| 青川县| 夏邑县|