将applicationContext拆分为多个文件

kos*_*ant 58 configuration spring

将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>
Run Code Online (Sandbox Code Playgroud)

实际问题:

  • 这种方法是正确/最好的吗?
  • 我真的需要同时指定中的配置位置DispatcherServlet context-param板块?

为了能够引用foo-servlet.xml从中定义的bean,我需要记住foo-service.xml什么?这是否与指定contextConfigLocation有关web.xml

更新1:

我正在使用Spring framework 3.0.我的理解是,我不需要像这样进行资源导入:

 <import resource="foo-services.xml"/> 
Run Code Online (Sandbox Code Playgroud)

这是正确的假设吗?

elj*_*nso 48

我发现以下设置最简单.

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

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

在您的情况下,只需intrafest-servlet.xmlWEB-INF目录中创建一个文件,而不需要在其中指定任何特定信息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>
Run Code Online (Sandbox Code Playgroud)

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

  • 我应该把foo-persistence.xml放在哪里,如何引用它的资源路径?进行战争时,找不到持久性单位。 (2认同)

Hum*_*ing 28

Mike Nereson在他的博客上说:

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

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

1. web.xml contextConfigLocation

您的第一个选择是通过ContextConfigLocation元素将它们全部加载到Web应用程序上下文中.假设您正在编写Web应用程序,那么您已经在这里拥有主要的applicationContext.您需要做的就是在下一个上下文的声明之间放置一些空格.

  <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>
Run Code Online (Sandbox Code Playgroud)

以上使用回车.或者,哟可以放入一个空间.

  <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>
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

你应该使用哪种策略?

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

因为,这允许我保持所有上下文彼此隔离.通过测试,我们可以只加载运行这些测试所需的上下文.这使得开发更加模块化,因为组件保持不变loosely coupled,以便将来我可以提取包或垂直层并将其移动到自己的模块.

2.如果要将上下文加载到a中non-web application,我会使用该import资源.

  • 复制文章http://blog.codehangover.com/load-multiple-contexts-into-spring/不引用. (10认同)

小智 14

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

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>
Run Code Online (Sandbox Code Playgroud)


小智 6

@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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢,希望这会有所帮助.