【问题标题】:Splitting applicationContext to multiple files将 applicationContext 拆分为多个文件
【发布时间】:2010-10-10 15:34:52
【问题描述】:

将Spring的配置拆分为多个xml文件的正确方法是什么?

目前有

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

我的web.xml 有以下内容:

<servlet>
    <description>Spring MVC Dispatcher Servlet</description>
    <servlet-name>intrafest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/foo-*.xml
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

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


<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

实际问题:

  • 这种方法正确/最佳吗?
  • 我真的需要在DispatcherServlet context-param 部分中指定配置位置吗?

我需要记住什么才能从foo-service.xml 引用foo-servlet.xml 中定义的bean?这与在web.xml 中指定contextConfigLocation 有关吗?

更新 1:

我正在使用 Spring 框架 3.0。据我了解,我不需要像这样进行资源导入:

 <import resource="foo-services.xml"/> 

这是一个正确的假设吗?

【问题讨论】:

    标签: spring configuration


    【解决方案1】:

    我发现以下设置最简单。

    使用DispatcherServlet的默认配置文件加载机制:

    框架将在初始化时 一个 DispatcherServlet,寻找一个 名为 [servlet-name]-servlet.xml 的文件 在您网站的 WEB-INF 目录中 应用程序并创建 bean 在那里定义(覆盖 定义的任何 bean 的定义 在全局范围内同名)。

    在您的情况下,只需在WEB-INF 目录中创建一个文件intrafest-servlet.xml,无需在web.xml 中指定任何特定信息。

    intrafest-servlet.xml 文件中,您可以使用import 来编写您的XML 配置。

    <beans>
      <bean id="bean1" class="..."/>
      <bean id="bean2" class="..."/>
    
      <import resource="foo-services.xml"/>
      <import resource="foo-persistence.xml"/>
    </beans>
    

    请注意,Spring 团队实际上更喜欢在创建 (Web)ApplicationContext 时加载多个配置文件。如果您仍然想这样做,我认为您不需要同时指定上下文参数 (context-param) servlet 初始化参数 (init-param)。两者之一就可以了。您还可以使用逗号指定多个配置位置。

    【讨论】:

    • +1 - 我的设置看起来像这样。虽然我认为这个设置与在 web.xml 中指定多个配置文件相比没有任何实际的优点/缺点 - 它看起来就像语义
    • 我绝对认为默认配置是有利的:约定优于配置。除了使用 extra config 指定多个配置文件,您只有一个 default “顶级”配置文件,它将导入您本来必须指定的相同文件。
    • 当你使用 import 时,注意不要多次导入同一个文件,(import inside import 等等),因为它会导致创建多个 bean 并导致难以发现的错误.
    • 我应该把foo-persistence.xml放在哪里,我应该如何引用它的资源路径?创建战争时,找不到持久性单位。
    【解决方案2】:

    Mike Nereson 在他的博客上这样说:

    http://blog.codehangover.com/load-multiple-contexts-into-spring/

    有几种方法可以做到这一点。

    1. web.xml contextConfigLocation

    您的第一个选择是将它们全部加载到您的 Web 应用程序中 上下文通过 ContextConfigLocation 元素。你已经走了 在这里有你的主要应用上下文,假设你正在写 一个网络应用程序。您需要做的就是在两者之间放置一些空白 下一个上下文的声明。

      <context-param>
          <param-name> contextConfigLocation </param-name>
          <param-value>
              applicationContext1.xml
              applicationContext2.xml
          </param-value>
      </context-param>
    
      <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
    

    以上使用回车。或者,你可以只放一个 空间。

      <context-param>
          <param-name> contextConfigLocation </param-name>
          <param-value> applicationContext1.xml applicationContext2.xml </param-value>
      </context-param>
    
      <listener>
          <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
      </listener>
    

    2。 applicationContext.xml 导入资源

    您的另一个选择是添加您的主要 applicationContext.xml 到 web.xml,然后在该主要上下文中使用 import 语句。

    applicationContext.xml,你可能有……

      <!-- hibernate configuration and mappings -->
      <import resource="applicationContext-hibernate.xml"/>
    
      <!-- ldap -->
      <import resource="applicationContext-ldap.xml"/>
    
      <!-- aspects -->
      <import resource="applicationContext-aspects.xml"/>
    

    您应该使用哪种策略?

    1.我总是喜欢通过web.xml加载。

    因为,这使我可以将所有上下文与每个上下文隔离开来 其他。通过测试,我们可以只加载我们需要运行的上下文 那些测试。这使得开发作为组件也更加模块化 留loosely coupled,这样以后我可以提取一个包 或垂直层并将其移动到自己的模块中。

    2.如果您将上下文加载到non-web application,我会使用import 资源。

    【讨论】:

    【解决方案3】:

    我们正在处理两种类型的上下文:

    1:根上下文(父上下文。通常包括所有jdbc(ORM,Hibernate)初始化和其他spring安全相关​​配置)

    2:单个 servlet 上下文(子上下文。通常是 Dispatcher Servlet 上下文并初始化所有与 spring-mvc 相关的 bean(控制器、URL 映射等))。

    这是一个包含多个应用程序上下文文件的 web.xml 示例

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
        <display-name>Spring Web Application example</display-name>
    
        <!-- Configurations for the root application context (parent context) -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->
                /WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->
            </param-value>
        </context-param>
    
        <!-- Configurations for the DispatcherServlet application context (child context) -->
        <servlet>
            <servlet-name>spring-mvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring/mvc/spring-mvc-servlet.xml
                </param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring-mvc</servlet-name>
            <url-pattern>/admin/*</url-pattern>
        </servlet-mapping>
    
    </web-app>

    【讨论】:

    • 很好的答案。区分根和子是关键。
    【解决方案4】:

    @eljenso : 如果应用程序使用 SPRING WEB MVC,将使用 intrafest-servlet.xml webapplication context xml。

    否则@kosoant 配置没问题。

    如果您不使用 SPRING WEB MVC,但想使用 SPRING IOC 的简单示例:

    在 web.xml 中:

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
    </context-param>
    

    然后,您的 application-context.xml 将包含:&lt;import resource="foo-services.xml"/&gt; 这些导入语句用于加载各种应用程序上下文文件并放入主 application-context.xml。

    谢谢,希望对您有所帮助。

    【讨论】:

      【解决方案5】:

      我是modular-spring-contexts 的作者。

      这是一个小型实用程序库,与使用 Composing XML-based configuration metadata 相比,它允许对 Spring 上下文进行更模块化的组织。 modular-spring-contexts 通过定义模块来工作,这些模块基本上是独立的应用程序上下文,并允许模块从其他模块导入 bean,这些模块在其原始模块中导出。

      那么重点是

      • 控制模块之间的依赖关系
      • 控制导出哪些 bean 以及在何处使用它们
      • 减少了 bean 命名冲突的可能性

      一个简单的例子如下所示:

      文件moduleDefinitions.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
          <context:annotation-config />
      
          <module:module id="serverModule">
              <module:config location="/serverModule.xml" />
          </module:module>
      
          <module:module id="clientModule">
              <module:config location="/clientModule.xml" />
              <module:requires module="serverModule" />
          </module:module>
      
      </beans>
      

      文件serverModule.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
          <context:annotation-config />
      
          <bean id="serverSingleton" class="java.math.BigDecimal" scope="singleton">
              <constructor-arg index="0" value="123.45" />
              <meta key="exported" value="true"/>
          </bean>
      
      </beans>
      

      文件clientModule.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
          <context:annotation-config />
      
          <module:import id="importedSingleton" sourceModule="serverModule" sourceBean="serverSingleton" />
      
      </beans>
      

      【讨论】:

      • 感谢@SpaceTrucker!好像你已经创造了我们也需要的东西。不知道为什么富春社区之前没有创造过类似的东西......
      猜你喜欢
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多