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

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

问题描述

将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 上下文参数部分?



我需要记住,能够引用 foo-servlet.xml中定义的bean 从 foo-service .xml ?这是否与在 web.xml

中指定 contextConfigLocation 有关系?

更新1:



我正在使用Spring 框架3.0。这是我的理解,我不需要这样的资源导入:

 < import resource =foo-services。 xml/>这是一个正确的假设吗?

>



使用 DispatcherServlet



< blockquote>

框架将在DispatcherServlet的初始化
上在WEB-INF中查找名为[servlet-name] -servlet.xml

文件目录下的
应用程序并创建定义的
(重写
定义的任何bean的
定义与
在全局范围中的相同的名称)。


在您的情况下,只需在<$ c $中创建一个文件 intrafest-servlet.xml c> web-INF dir,并且不需要在 web.xml 中指定任何特定信息。



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

 < bean> 
< bean id =bean1class =.../>
< bean id =bean2class =.../>

< import resource =foo-services.xml/>
< import resource =foo-persistence.xml/>
< / beans>请注意,Spring团队实际上更喜欢在创建(Web)ApplicationContext时加载多个配置文件。)


$ b。如果你还想这样做,我想你不需要同时指定上下文参数( context-param servlet初始化参数( init-param )。其中一个会做。您也可以使用逗号来指定多个配置位置。


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

At the moment I have

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

My web.xml has the following:

<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 DispatcherServlet AND the context-param sections?

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?

Update 1:

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.

Use the default config file loading mechanism of 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).

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.

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>

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天全站免登陆