请问在spring开发中,如何处理多个配置文件的问题

marcoleung 2005-03-04 03:50:38
项目是B/S架框,团队开发,为方便开发人员开发工作,将配置文件拆成多个,但现在问题是如何将这些配置文件自动加载进来生成一个applicationContext.我翻了一下资料,spring提供了一个专用在web应用下的工具类,只要在web.xml上配置一个servlet就可以了,但问题是这个类只懂从一个固定地方上找一个配置文件。后来听说有一个叫ClassPathApplicationContext类,它可以自动加载classpath下的多个配置文件,但查看文档的时候好像说这个类只是在测试用的。请问大家有没有更好的方法。
...全文
630 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hfree 2005-03-24
  • 打赏
  • 举报
回复
先给daquan198163(大权) 道个谢,上次你回我贴中说到Equinox这一个项目,我也下来看了一下.
发现其配置多个配置文件如下!可以说得上是另一种方法吧.只是遵循一种命名规则!

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml,其他的配置类似.这样就可以加载了,不必写用空格或是逗号分开!

marcoleung 2005-03-05
  • 打赏
  • 举报
回复
经过一番学习,总结出了使用多个spring配置文件的方法,望对各位有帮助。

public class ContextLoader
extends Object
Performs the actual initialization work for the root application context. Called by ContextLoaderListener and ContextLoaderServlet.

Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type, falling back to the default of XmlWebApplicationContext if not found. With the default ContextLoader implementation, any context class specified needs to implement ConfigurableWebApplicationContext.

Passes a "contextConfigLocation" context-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "applicationContext1.xml, applicationContext2.xml". If not explicitly specified, the context implementation is supposed to use a default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using one of Spring's default ApplicationContext implementations. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

Above and beyond loading the root application context, this class can optionally load or obtain and hook up a shared parent context to the root application context. See the loadParentContext(ServletContext) method for more information.

1.使用监听器或servlet
这两种方式都可以处理多个配置文件的问题,区别见上。
要使用多个配置文件只需在web.xml中加入一个
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>
多个配置文件用逗号或空格分开即可,具体原因请见上面ContextLoader的描述

2.使用ClassPathApplicationContext。这个类会自动从classpath目录中加载所有的配置文件,不过这个类好像只适用本地调试时用的。

3.通过一个父配置文件将所有子配置文件导入。
在配置文件中有一个标签import,它能把其它的bean定义配置文件导入到父文件夹中

4.通过FileSystemXmlApplicationContext
FileSystemXmlApplicationContext的构造函数是一个字符串数组这个数组就是保存配置文件的路径

marcoleung 2005-03-05
  • 打赏
  • 举报
回复
查了一下资料,用监听器只是因为容器的版本问题,如果是servlet2.4的容器,建议使用监听器方式,如果是servlet2.2 2.3就是可以使用ContextLoaderServlet。

这里有一个问题,监听器方式可以解决多个配置文件问题吗?ContextLoaderServlet可以解决吗?我正在试。
marcoleung 2005-03-04
  • 打赏
  • 举报
回复
别光说别人的错,懂得就发表一下。
_chage 2005-03-04
  • 打赏
  • 举报
回复
楼上的,plugin的方式现在比较落伍了,如果需要在容器中加载,请使用监听器,
Vincent2013SH 2005-03-04
  • 打赏
  • 举报
回复
struts-config.xml里面加这个
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml,,,,,,,"/>
这是《Spring Live》Sample Chapter.pdf里的例子
amendajing 2005-03-04
  • 打赏
  • 举报
回复
mark

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧