隨筆-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會提前在容器中初使化一些實例,但用戶使用時,再從容器中取一個給用戶。  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 修文县| 尉犁县| 云霄县| 祁门县| 山东| 德格县| 体育| 牙克石市| 陆丰市| 景洪市| 顺平县| 营口市| 江源县| 浦江县| 高州市| 会同县| 西峡县| 绵竹市| 揭西县| 邵阳县| 宿州市| 苏尼特左旗| 来凤县| 龙游县| 右玉县| 西贡区| 麟游县| 驻马店市| 萨嘎县| 万宁市| 怀来县| 华阴市| 西平县| 拜城县| 武穴市| 错那县| 天气| 宁河县| 那曲县| 塔城市| 高淳县|