瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          注意spring, singleton不single! ?

          Posted on 2010-08-30 00:41 瘋狂 閱讀(6915) 評論(12)  編輯  收藏 所屬分類: java 、spring 、web
            spring的singleton真的會在整個應用中創建單一的對象嗎?非也,他只會在內部springframework的一個applicationContext上下文中保持單利,但是如果有兩個applicationContext呢?

           首先看以下實例 :
            
           首先定義一個bean 并注冊為單利,非懶加載對象:
           
          package com.joe.service;


          public class TestService {

              
          public TestService(){
                  System.out.println(
          "create  TestService ");
              }

          }


          配置:
          <bean id="testservice" class="com.joe.service.TestService" scope="singleton" lazy-init="false"></bean>

          測試代碼:
              private static ApplicationContext context1;
              
          private static ApplicationContext context2;
              
              
          static{
                  context1 
          = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-bean_*.xml"});
                  context2 
          = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-bean_*.xml"});
                  
              }
              
              
          public static void main(String[] args) throws SQLException, IOException {
                  
                  System.out.println(
          "context1 中 testservice 是否單利?"+context1.isSingleton("testservice"));
                  
                  TestService service 
          = (TestService) context1.getBean("testservice");
                  
                  System.out.println(service);
                  
                  System.out.println(
          "context2  中 testservice 是否單利?"+context2.isSingleton("testservice"));
                  
                  TestService service2 
          = (TestService) context2.getBean("testservice");
                  
                  System.out.println(service2);
                  System.out.println(service);
                  
                  System.out.println(
          "context1 中 testservice 是否和context2 中 testservice 是一個對象?"+(service==service2));//關鍵地方 兩個ApplicationContext 中拿 到的是否是一個對象?
              }

          結果:
          create  TestService 
          create  TestService 

          context1 中 testservice 是否單利?
          true
          com.joe.service.TestService@54c4ad
          context2  中 testservice 是否單利?
          true
          com.joe.service.TestService@13c7378
          com.joe.service.TestService@54c4ad
          context1 中 testservice 是否和context2 中 testservice 是一個對象?
          false

           很明顯在一個jvm應用中出現兩個被標注為單利的實例。
              spring內部通過上下文中的集合來管理對象,核心為beanFactory,而非通過asm等來動態改變類結構來控制類的scope。

               我們跟著源碼來看看:
            在調用new ClassPathXmlApplicationContext實際上調用了
            this(configLocations, true, null);其中的true代表要刷新上下文:而最終的refresh是這樣的
           
          if (hasBeanFactory()) {
                      destroyBeans();
                      closeBeanFactory();
                  }

          但是很費解的是hasBeanFactory的判斷:
          protected final boolean hasBeanFactory() {
                  
          synchronized (this.beanFactoryMonitor) {
                      
          return (this.beanFactory != null);
                  }

              }

          其中的this.beanFactory是ClassPathXmlApplicationContext的實例的屬性private DefaultListableBeanFactory beanFactory;
            不用說this.beanFactory==null因為我們new了一個ClassPathXmlApplicationContext , beanFactory也沒有初始化操作,也沒有parentContext (因為我根本不知道有沒有parentContext)而beanFactory也不是static的。
            也就是說在第二次new  ClassPathXmlApplicationContext的時候并不會refresh beanFactory 當然就會有兩個單利的實例。
            
              
             

              此問題在項目中出現,當時由于一個同事在自己的一個類里面的static區域new了ClassPathXmlApplicationContext用來測試忘記刪掉, 而我們的所有類又是通過web容器來加載,結果就導致項目中出現單利對象出現兩個的問題,結果害的那個苦?。?br />    
             希望大家在實際項目中注意到,也希望大家跟帖討論下,我分析的是否正確,大家實際項目中有沒有解決方案來避免這樣的問題!

          評論

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 10:01 by 隔葉黃鶯
          謝謝,又了解到一點知識,范圍是基于 applicationContext,而不是 JVM。其實通常的單例,我們要更嚴格的來講也不能說是基于 JVM,而是基于 ClassLoader 的。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 10:38 by forest
          我測試了一下,發現兩個test是相同的。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 10:58 by @joe
          @forest
          能提供下測試代碼看看嗎?

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 11:11 by Lancelot
          人家的singleton本來就是相對于容器的。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 11:43 by @joe
          @Lancelot
          但是真正的項目中要的是真正的單利,我感覺這種思想脫離了實際應用。給項目帶來了麻煩這是事實。我不知道spring是處于何種思想?希望兄臺給個解釋!

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 11:57 by @joe
          查看spring的文檔然后了解到實質,請看一下文檔(來自spring官方文檔)
          3.4.1. The singleton scope
          When a bean is a singleton, only one shared instance of the bean will be managed, and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned by the Spring container.

          To put it another way, when you define a bean definition and it is scoped as a singleton, then the Spring IoC container will create exactly one instance of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and all subsequent requests and references for that named bean will result in the cached object being returned.


          請注意一下內容:
          **********************************************
          Please be aware that Spring's concept of a singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hard codes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean.
          *********************************************
          This means that if you define one bean for a particular class in a single Spring container, then the Spring container will create one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring

          當然spring是per container and per bean.
          也就是(Scopes a single bean definition to a single object instance per Spring IoC container.
          );
          唉 以后注意。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 12:02 by @joe
          @@joe
          但為什么不使用GoF Singleton 呢??????

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 12:29 by anders
          @joe
          但是真正的項目中要的是真正的單利,我感覺這種思想脫離了實際應用。給項目帶來了麻煩這是事實。我不知道spring是出于何種思想?希望兄臺給個解釋!

          ...
          有點無語。

          第一joe沒有弄清出spring提供單例范圍。spring單例基于container的。
          第二joe還沒有弄清項目中單例的范圍,“什么叫真正的項目真正的單例”,以偏蓋全——以自己項目的需求覆蓋全部的需求。

          對于一個J2EE來說,如果運行在應用服務器下,一個應用服務器可以同時支持多個應用,單例的范圍就應該是每個應用一個單例,就是spring的考慮,不能是
          兩個應用使用同一個單例,會出很多問題比如classload的問題。如果確實需要,則應自己擴展spring的scope,采用jndi的方式獲取單例。

          “什么叫真正的項目真正的單例”,單例是基于JVM的,還是基于OS的,還是基于內網的,還是基于整個互聯網的。

          基于JVM的給用靜態變量;基于OS的給借助OS特性,比如文件系統;基于內網的和互聯網的就根復雜了。。。。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-30 13:22 by @joe
          @anders
          首先感謝anders的恢復 !,讓人更深入的了解了Singleton !
          但是我的情況就是在一個應用中出現了這樣的問題,既然一個應用中可以new 多個applicationContext,為什么不加以控制呢!我感覺不應該研發自己去注意這些東西,應該對于一個應用有一個Global Application足以,如果再new application的時候 直接指向前一個applicationContext!這樣Singleton 在一個應用中應該可以得到解決!

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-31 11:16 by xylz
          @@joe
          anders說的很明白了,spring是基于container的!這與單例的作用范圍其實沒有關系。。。

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2010-08-31 13:44 by @joe
          @xylz
          spring是基于container的.我明白!
          但是脫離實際項目談這些是沒有用,給項目帶來風險的東西,都是值得考慮,值得注意的,概念誰都懂!

          # re: 注意spring, singleton不single! ?  回復  更多評論   

          2013-12-21 16:33 by zb
          爭論有意義嗎,說明白不就成了,孔已己似的.
          主站蜘蛛池模板: 修水县| 马关县| 安宁市| 玉田县| 襄城县| 宝应县| 汾西县| 九江县| 习水县| 咸阳市| 吐鲁番市| 惠州市| 齐齐哈尔市| 桂平市| 翁牛特旗| 梨树县| 大连市| 潮安县| 竹溪县| 三明市| 云林县| 城固县| 吉水县| 突泉县| 灵石县| 固阳县| 谢通门县| 丰都县| 牙克石市| 达拉特旗| 康乐县| 漳浦县| 柳江县| 巴林左旗| 小金县| 邮箱| 天全县| 元阳县| 巴林右旗| 绥棱县| 汉川市|