spring 将 applicationContext 拆分为多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/600095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting applicationContext to multiple files
提问by kosoant
What is the correct way to split Spring's configuration to multiple xml files?
将 Spring 的配置拆分为多个 xml 文件的正确方法是什么?
At the moment I have
目前我有
/WEB-INF/foo-servlet.xml/WEB-INF/foo-service.xml/WEB-INF/foo-persistence.xml
/WEB-INF/foo-servlet.xml/WEB-INF/foo-service.xml/WEB-INF/foo-persistence.xml
My web.xmlhas the following:
我的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>
The actual questions:
实际问题:
- Is this approach correct/best?
- Do I really need to specify the config locations both in the
DispatcherServletANDthecontext-paramsections?
- 这种方法是否正确/最好?
- 我真的需要同时指定中的配置位置
DispatcherServlet和该context-param板块?
What do I need to keep in mind to be able to reference beans defined in foo-servlet.xmlfrom foo-service.xml? Does this have something to do with specifying contextConfigLocationin web.xml?
我需要记住什么才能引用foo-servlet.xmlfrom 中定义的 bean foo-service.xml?这与指定contextConfigLocationin 有关系web.xml吗?
Update 1:
更新 1:
I'm using Springframework 3.0. It's my understanding that I don't need to do resource importing like this:
我正在使用Spring框架 3.0。我的理解是我不需要像这样导入资源:
<import resource="foo-services.xml"/>
Is this a correct assumption?
这是一个正确的假设吗?
采纳答案by eljenso
I find the following setup the easiest.
我发现以下设置最简单。
Use the default config file loading mechanism of DispatcherServlet:
使用DispatcherServlet的默认配置文件加载机制:
The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).
在初始化 DispatcherServlet 时,框架将在 Web 应用程序的 WEB-INF 目录中查找名为 [servlet-name]-servlet.xml 的文件,并创建在那里定义的 bean(覆盖使用全局范围内的相同名称)。
In your case, simply create a file intrafest-servlet.xmlin the WEB-INFdir and don't need to specify anything specific information in web.xml.
在您的情况下,只需intrafest-servlet.xml在WEB-INFdir 中创建一个文件,而无需在web.xml.
In intrafest-servlet.xmlfile you can use importto compose your XML configuration.
在intrafest-servlet.xml文件中,您可以使用导入来编写您的 XML 配置。
<beans>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
<import resource="foo-services.xml"/>
<import resource="foo-persistence.xml"/>
</beans>
Note that the Spring team actually prefers to load multiple config files when creating the (Web)ApplicationContext. If you still want to do it this way, I think you don't need to specify both context parameters (context-param) andservlet initialization parameters (init-param). One of the two will do. You can also use commas to specify multiple config locations.
请注意,Spring 团队实际上更喜欢在创建 (Web)ApplicationContext 时加载多个配置文件。如果你还想这样做,我认为你不需要同时指定上下文参数(context-param)和servlet 初始化参数(init-param)。两者之一会做。您还可以使用逗号来指定多个配置位置。
回答by Human Being
Mike Nereson has this to say on his blog at:
Mike Nereson 在他的博客上说:
http://blog.codehangover.com/load-multiple-contexts-into-spring/
http://blog.codehangover.com/load-multiple-contexts-into-spring/
There are a couple of ways to do this.
1. web.xml contextConfigLocation
Your first option is to load them all into your Web application context via the ContextConfigLocation element. You're already going to have your primary applicationContext here, assuming you're writing a web application. All you need to do is put some white space between the declaration of the next context.
<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>The above uses carriage returns. Alternatively, yo could just put in a space.
<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 import resource
Your other option is to just add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.
In
applicationContext.xmlyou might have…<!-- hibernate configuration and mappings --> <import resource="applicationContext-hibernate.xml"/> <!-- ldap --> <import resource="applicationContext-ldap.xml"/> <!-- aspects --> <import resource="applicationContext-aspects.xml"/>Which strategy should you use?
1.I always prefer to load up via web.xml.
Because , this allows me to keep all contexts isolated from each other. With tests, we can load just the contexts that we need to run those tests. This makes development more modular too as components stay
loosely coupled, so that in the future I can extract a package or vertical layer and move it to its own module.2.If you are loading contexts into a
non-web application, I would use theimportresource.
有几种方法可以做到这一点。
1. web.xml 上下文配置位置
您的第一个选择是通过 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>以上使用回车。或者,你可以只放一个空格。
<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资源。
回答by user2815238
There are two types of contexts we are dealing with:
我们正在处理两种类型的上下文:
1: root context (parent context. Typically include all jdbc(ORM, Hibernate) initialisation and other spring security related configuration)
1:根上下文(父上下文。通常包括所有jdbc(ORM,Hibernate)初始化和其他与spring安全相关的配置)
2: individual servlet context (child context.Typically Dispatcher Servlet Context and initialise all beans related to spring-mvc (controllers , URL Mapping etc)).
2:单独的servlet上下文(子上下文。通常是Dispatcher Servlet上下文并初始化与spring-mvc相关的所有bean(控制器,URL映射等))。
Here is an example of web.xml which includes multiple application context file
这是包含多个应用程序上下文文件的 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>
回答by Sreeni
@eljenso : intrafest-servlet.xml webapplication context xml will be used if the application uses SPRING WEB MVC.
@eljenso :如果应用程序使用 SPRING WEB MVC,将使用 intrafest-servlet.xml webapplication context xml。
Otherwise the @kosoant configuration is fine.
否则@kosoant 配置很好。
Simple example if you dont use SPRING WEB MVC, but want to utitlize SPRING IOC :
如果您不使用 SPRING WEB MVC,但想使用 SPRING IOC 的简单示例:
In web.xml:
在 web.xml 中:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
Then, your application-context.xml will contain: <import resource="foo-services.xml"/>these import statements to load various application context files and put into main application-context.xml.
然后,您的 application-context.xml 将包含:<import resource="foo-services.xml"/>这些导入语句以加载各种应用程序上下文文件并放入主 application-context.xml。
Thanks and hope this helps.
谢谢并希望这会有所帮助。
回答by SpaceTrucker
I'm the author of modular-spring-contexts.
我是modules-spring-contexts 的作者。
This is a small utility library to allow a more modular organization of spring contexts than is achieved by using Composing XML-based configuration metadata. modular-spring-contextsworks by defining modules, which are basically stand alone application contexts and allowing modules to import beans from other modules, which are exported ín their originating module.
这是一个小型实用程序库,与使用Composing XML-based configuration metadata相比,它允许对 spring 上下文进行更模块化的组织。modular-spring-contexts通过定义模块来工作,这些模块基本上是独立的应用程序上下文,并允许模块从其他模块导入 bean,这些 bean 在它们的原始模块中导出。
The key points then are
那么关键点是
- control over dependencies between modules
- control over which beans are exported and where they are used
- reduced possibility of naming collisions of beans
- 控制模块之间的依赖关系
- 控制导出哪些 bean 以及在何处使用它们
- 减少 bean 命名冲突的可能性
A simple example would look like this:
一个简单的例子如下所示:
File moduleDefinitions.xml:
文件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>
File serverModule.xml:
文件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>
File clientModule.xml
文件 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>

