隨筆-26  評論-44  文章-20  trackbacks-0
          public class Table {
              String tableName;
              String mainPropertyName;
              static int count=1;
              static DBExcute dbExcute=(DBExcute)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("dbExcute");
             
              Table(String tableName,String mainPropertyName){
                  this.tableName=tableName;
                  this.mainPropertyName=mainPropertyName;
                  System.out.println("construct table"+(count++)+" use:"+dbExcute);
              }
                  .............
                  .............
               public static void main(String []dsf){
               new Table("user","1");
            }
          }

          如上只new Table(..)一次:
          結果卻是:
          construct table1 use:null
          construct table2 use:null
          construct table3 use:null
          construct table4 use:null
          construct table5 use:null
          construct table6 use:com.yijia_ctgu.DB.DBExcute@535b58


          奇怪中。。。。。。。


          明白了,spring容器初始化的時候,初始化dbDAO bean會生成五個table,可能是先初始化,后才賦值給dbExcute,所以剛開始的五個都是null.

          posted on 2008-11-05 14:45 開機 閱讀(277) 評論(1)  編輯  收藏

          評論:
          # re: 為何new了一次,卻執行了好幾次? 2008-11-18 18:40 | eyejava
          工廠ApplicationContext會提前在容器中初使化一些實例,但用戶使用時,再從容器中取一個給用戶。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 青海省| 普兰店市| 凤冈县| 静海县| 定兴县| 酒泉市| 溧水县| 拉萨市| 友谊县| 安徽省| 浠水县| 宁南县| 墨竹工卡县| 洪洞县| 定远县| 肇东市| 邮箱| 威宁| 永昌县| 新宾| 宁强县| 原阳县| 冀州市| 湘西| 横峰县| 台南市| 内乡县| 中卫市| 丹江口市| 阳城县| 名山县| 石景山区| 宜州市| 望都县| 张家港市| 海南省| 兴义市| 米林县| 武威市| 宁强县| 南川市|