将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
AND context-param
部分指定配置位置?我需要记住,能够从foo-servlet.xml
引用foo-service.xml
中定义的bean吗?这是否与在contextConfigLocation
中指定web.xml
?
更新1:
我正在使用 Spring 框架3.0。我的理解是,我不需要像这样进行资源导入:
<import resource="foo-services.xml"/>
这是正确的假设吗?
答案 0 :(得分:47)
我发现以下设置最简单。
使用DispatcherServlet的默认配置文件加载机制:
框架将在初始化时进行 一个DispatcherServlet,找一个 文件名为[servlet-name] -servlet.xml 在您的Web的WEB-INF目录中 应用程序并创建bean 在那里定义(覆盖 定义的任何bean的定义 全球范围内的相同名称。)
在您的情况下,只需在intrafest-servlet.xml
目录中创建文件WEB-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>
请注意,Spring团队实际上更喜欢在创建(Web)ApplicationContext时加载多个配置文件。如果您仍然希望这样做,我认为您不需要同时指定上下文参数(context-param
)和 servlet初始化参数(init-param
)。其中一个会做。您还可以使用逗号指定多个配置位置。
答案 1 :(得分:27)
Mike Nereson在他的博客上说:
http://blog.codehangover.com/load-multiple-contexts-into-spring/
有几种方法可以做到这一点。
<强> 1。 web.xml contextConfigLocation
您的第一个选择是将它们全部加载到Web应用程序中 上下文通过ContextConfigLocation元素。你已经去了 假设您正在写作,请将您的主要applicationContext放在这里 一个Web应用程序。你需要做的只是在它们之间留一些空白区域 宣布下一个背景。
<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
资源。
答案 2 :(得分:13)
我们正在处理两种类型的上下文:
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>
答案 3 :(得分: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>
然后,您的application-context.xml将包含:<import resource="foo-services.xml"/>
这些import语句用于加载各种应用程序上下文文件并放入main application-context.xml。
谢谢,希望这会有所帮助。
答案 4 :(得分:1)
我是modular-spring-contexts的作者。
这是一个小型实用程序库,与使用Composing XML-based configuration metadata相比,可以实现更加模块化的弹簧上下文组织。 modular-spring-contexts
通过定义模块来工作,模块基本上是独立的应用程序上下文,并允许模块从其他模块导入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>