如果說BeanFactory是spring的心臟,那么Application就是完整的身軀。ApplicationContext就是由BeanFactory派生出來的。
一、ApplicationContext類
ApplicationContext的主要實現類是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默認從類路徑加載配置 文件,后者默認 從文件系統加載文件。
1)如果配置文件放在類路徑下,直接使用ClassPathXmlApplicationContext實現類:
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");
這里的參數等同於:"classpath:com/techman/context/beans.xml"
2)如果配置文件在文件系統的路徑下,則可以優先考慮使用FileSystemXmlApplicationContext實現類:
ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");
這里的參數等同於:"file:com/techman/context/beans.xml".
3)如果有多個配置文件,獲取ApplicationContext對象時還可以指定一組配置文件,Spring自動將多個配置文件在內存中整合成一個配置文件:
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});
或者ApplicationContext ctx=new ClassPathXmlApplicationContext("conf/bean*.xml");
二、如何配置多個applicationContext.xml?
即,在啟動容器的時候如何引入多個applicationContext.xml?
在web.xml中將applicationContext.xml作為當前web應用的初始化參數,多個applicationContext.xml用逗號隔開,也可以使用
例如:

或者

三、一個applicationContext.xml文件如何引用另一個applicationContext.xml文件中的bean?
在需要引用其它applicationContext.xml文件的applicationContext.xml文件中的<beans></beans>標記之間引入別人的
applicationContext.xml,格式如下:
<beans>
<import resource="applicationContext-hibernate.xml"/>
</beans>
(其中,<inport> 相當於Java中的導包)
需要注意的是:
<import resource="applicationContext-hibernatee.xml"/>這一句要放在所有bean配置的最前面。
四、如何引用別人已經配置好了的bean呢?
例如,將另外一個人配置的commonDAO注入給自己的biz中:配置如下:
<bean id="cstServiceBiz" class="org.jb.t0821c.biz.CstServiceBiz">
<property name="commonDAO">
<ref bean="commonDAO"/>
</property>
</bean>
