当遇到项目需要细化xml配置的分类时,可以通过下面方式完成对配置文件的装配:
1、通过枚举
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value>, <param-value>classpath:applicationContext2.xml</param-value>, <param-value>classpath:applicationContext3.xml</param-value> </context-param>
或通配符
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value>, </context-param>
2、通过import文件引入实现主分支文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 然后在applicationContext.xml路径下通过添加对应的Import引入分支文件 <import resource= "classpath:applicationContext2.xml"> <import resource= "classpath:applicationContext3.xml">
在spring框架中引入配置文件的做法也有不同:
普通java工程中:ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
在WEB工程中也可以按照上面的方式引入,但更常见的引入方式为:
ApplicationContext ac1 =WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)`