将 applicationContext 拆分为多个文件 [英] Splitting applicationContext to multiple files

查看:43
本文介绍了将 applicationContext 拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the correct way to split Spring's configuration to multiple xml files?

目前我有

  • /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 部分中指定配置位置吗?
  • Is this approach correct/best?
  • Do I really need to specify the config locations both in the DispatcherServlet AND the context-param sections?

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

What do I need to keep in mind to be able to reference beans defined in foo-servlet.xml from foo-service.xml? Does this have something to do with specifying contextConfigLocation in web.xml?

更新 1:

我使用的是 Spring 框架 3.0.我的理解是我不需要像这样导入资源:

I'm using Spring framework 3.0. It's my understanding that I don't need to do resource importing like this:

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

这是一个正确的假设吗?

Is this a correct assumption?

推荐答案

我觉得以下设置最简单.

I find the following setup the easiest.

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

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

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).

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

In your case, simply create a file intrafest-servlet.xml in the WEB-INF dir and don't need to specify anything specific information in web.xml.

intrafest-servlet.xml 文件中,您可以使用 import 以组成您的 XML 配置.

In intrafest-servlet.xml file you can use import to compose your XML configuration.

<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-参数).两者之一会做.您还可以使用逗号来指定多个配置位置.

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) and servlet initialization parameters (init-param). One of the two will do. You can also use commas to specify multiple config locations.

这篇关于将 applicationContext 拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆