将 applicationContext 拆分为多个文件
-
03-07-2019 - |
题
将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
部分?
我需要记住什么才能引用定义的bean foo-servlet.xml
从 foo-service.xml
?这与指定有关吗 contextConfigLocation
在 web.xml
?
更新1:
我在用着 春天 框架3.0。据我了解,我不需要像这样进行资源导入:
<import resource="foo-services.xml"/>
这是一个正确的假设吗?
解决方案
我发现以下设置最简单。
使用默认的配置文件加载机制 调度程序Servlet:
该框架在初始化时 的 DispatcherServlet 中查找 名为 [servlet-name]-servlet.xml 的文件 的 WEB-INF 目录中。 应用程序并创建 Bean 定义的 定义的任何 bean 的定义 在全局作用域中的名称相同)。
对于您的情况,只需创建一个文件 intrafest-servlet.xml
在里面 WEB-INF
dir 并且不需要在其中指定任何特定信息 web.xml
.
在 intrafest-servlet.xml
您可以使用的文件 进口 编写您的 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
)。两者之一即可。您还可以使用逗号指定多个配置位置。
其他提示
迈克·内森 (Mike Nereson) 在他的博客上说道:
http://blog.codehangover.com/load-multiple-contexts-into-spring/
有几种方法可以做到这一点。
1.web.xml 上下文配置位置
第一种方法是将它们全部加载到网络应用程序中 通过 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,然后在主上下文中使用导入语句。
在
applicationContext.xml
你可能有…<!-- hibernate configuration and mappings --> <import resource="applicationContext-hibernate.xml"/> <!-- ldap --> <import resource="applicationContext-ldap.xml"/> <!-- aspects --> <import resource="applicationContext-aspects.xml"/>
您应该使用哪种策略?
1. 我总是喜欢通过加载 网络.xml.
因为,这可以让我保持所有上下文相互隔离 其他。通过测试,我们可以只加载需要运行的上下文 这些测试。这也使开发更加模块化,因为组件 逗留
loosely coupled
, 这样,将来我就可以提取一个软件包 或垂直层,并将其移动到自己的模块中。2. 如果您要将上下文加载到
non-web application
, ,我会用import
资源。
我们正在处理两种类型的上下文:
1: :根上下文(父上下文。通常包括所有 jdbc(ORM、Hibernate) 初始化和其他 spring security 相关配置)
2: :单独的 servlet 上下文(子上下文。通常是 Dispatcher Servlet Context 并初始化与 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>
@eljenso:intrafest-servlet.xml 如果应用程序使用 SPRING WEB MVC,则将使用 web 应用程序上下文 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"/>
这些导入语句加载各种应用程序上下文文件并放入主 application-context.xml 中。
谢谢并希望这有帮助。
我是作者 模块化弹簧上下文.
这是一个小型实用程序库,与使用所实现的相比,它允许对 spring 上下文进行更加模块化的组织 编写基于 XML 的配置元数据. 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>