隨筆-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 開機 閱讀(270) 評論(1)  編輯  收藏

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

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


          網站導航:
           
          主站蜘蛛池模板: 富阳市| 镇巴县| 永州市| 闽侯县| 曲阳县| 兴宁市| 漳州市| 鄂伦春自治旗| 中西区| 和龙市| 台前县| 天门市| 博爱县| 万载县| 陈巴尔虎旗| 龙里县| 股票| 钦州市| 延寿县| 台北县| 开封市| 图木舒克市| 奈曼旗| 隆昌县| 镇赉县| 新邵县| 雷波县| 昌黎县| 昭觉县| 同德县| 江阴市| 确山县| 夏河县| 互助| 双城市| 大洼县| 丰镇市| 民乐县| 进贤县| 介休市| 原平市|