随笔-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 开机 阅读(249) 评论(1)  编辑  收藏

评论:
# re: 为何new了一次,却执行了好几次? 2008-11-18 18:40 | eyejava
工厂ApplicationContext会提前在容器中初使化一些实例,但用户使用时,再从容器中取一个给用户。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: