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里的例子

解决方案 »

  1.   

    楼上的,plugin的方式现在比较落伍了,如果需要在容器中加载,请使用监听器,
      

  2.   

    查了一下资料,用监听器只是因为容器的版本问题,如果是servlet2.4的容器,建议使用监听器方式,如果是servlet2.2 2.3就是可以使用ContextLoaderServlet。这里有一个问题,监听器方式可以解决多个配置文件问题吗?ContextLoaderServlet可以解决吗?我正在试。
      

  3.   

    经过一番学习,总结出了使用多个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的构造函数是一个字符串数组这个数组就是保存配置文件的路径
      

  4.   

    先给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,其他的配置类似.这样就可以加载了,不必写用空格或是逗号分开!