2006年12月26日
使用new是不是編譯期就加載類到內(nèi)存中?
為什么設(shè)計類時一般不推薦使用new,而推薦使用運行期再加載類的方式(Class.forName),比如Spring框
架就完全不用new。
Spring中,使用以下方式獲得bean的引用:
ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
Action action = (Action) ctx.getBean("TheAction");
其中g(shù)etBean()函數(shù)的主要作用是否是加載類,并返回對象的引用?它的主要代碼是否為以下代碼呢?:
Class c=Class.forName("TheAction");
Object o=c.newInstance();
return o;
有待查看Spring的源代碼驗證自己的猜測!!!