首先我们都知道要在web项目中通过容器自动加载spring的配置信息,则需要在web.xml中增加如下代码:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring是如何加载配置文件肯定也跟 ContextLoaderListener类有关,该类可以作为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml加上面代码即可。
如果有多个配置文件需要载入,则考虑使用<context-param>即元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时参数名字应该是contextConfigLocation。所以context-param参数的名字是固定的contextConfigLocation.
比如下面的示例:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
</param-value>
</context-param>
另外还这样这样走,在applicationContext.xrnl 文件中加入自定义的多个配置文件,如下所示:
<beans>
<import resource="classpath:applicationContext-db.xml" />
<import resource="classpath:applicationContext-order.xml" />
<import resource="classpath:applicationContext-product.xml" />
<import resource="classpath:applicationContext-pay.xml" />
<import resource="classpath:applicationContext-activity.xml" />
<import resource="classpath:applicationContext-logo.xml" />
<import resource="classpath:applicationContext-paycar.xml" />
</beans>
这样的话,只需在web.xml中引入配置文件 applicationContext.xrnl 即可。
多个配置文件用","分开,也可以使用通配符"*"加载多个配置文件。如上例!
如果是直接在java代码中加载多个配置文件该怎么做呢?请看下面实例:
- ApplicationContext context= new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});
相关链接参考:http://blog.sina.com.cn/s/blog_5c0522dd0100crpp.html
分享到:
相关推荐
本文档是很专业的 Spring动态加载配置文件,实现热部署方案。
在spring中加载多个配置文件的方法,希望对你有用。
Spring3.0 配置文件中加载Properties文件的小例子
本文详细介绍了Spring中如何加载多个配置文件
主要介绍了浅谈SpringBoot2.4 配置文件加载机制大变化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring 加载多个配置文件
springmvc项目中如何加载jar包中的spring配置文件
本文档介绍了spring中的在classpath中加载配置文件
加载spring配置文件,提供getBean接口.
Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。 下面列举的是一份比较完整的配置文件模板,...
该项目展示spring配置文件的那些bean的加载顺序
使用spring配置文件配置dubbo服务,ioc注入。不是其他入门实例的api启动。 每一步可能犯的错误都在下面的地址里有详细描述 详细介绍参见blog http://blog.csdn.net/fangxiaoji/article/details/41845955
spring 启动时根据配置文件的不同加载不同的文件
spring整合springmvc、mybatis的jar包和配置文件,spring整合springmvc、mybatis的jar包和配置文件,spring整合springmvc、mybatis的jar包和配置文件
Spring@PropertySource 配置文件自定义加密、自定义Yaml文件加载
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
spring boot中配置mybatis xml资源文件热加载的方法以及相关文件
本篇文章主要介绍了spring boot加载第三方jar包的配置文件的方法,详细的介绍了spring boot jar包配置文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下