Spring bean配置文件在Maven WAR模块中放在哪里? [英] Where do Spring bean configuration files go in a Maven WAR module?

查看:1223
本文介绍了Spring bean配置文件在Maven WAR模块中放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一堆示例项目,我似乎不能找出一个常见的最佳实践。我看到Spring bean配置文件有时会在 src / main / webapp / WEB-INF 目录中。我已经看到这与 web.xml 中的Servlet定义一起,如下所示:

 < servlet> 
< servlet-name> my-stuff< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/my-stuff-servlet.xml< / param-value>
< / init-param>
< load-on-startup> 1< / load-on-startup>
< / servlet>但是我也看到了包含在 web.xml中的bean配置文件 / code>顶级 - 即在Servlet之外。这是什么意思?这是针对交叉Servlet bean吗?有时它在 src / main / webapp / WEB-INF 目录中,有时它位于 src / main / resources 。另外,我已经看到在WAR模块中定义的其他bean配置文件只是在 src / main / resources 中的一切。



我已经阅读并重新读取Spring文档,但我发现的唯一约定是,默认情况下一个Servlet上下文配置文件应该在 src / main / webapp / WEB-INF 目录 {servlet-name} -servlet.xml



为什么?

解决方案

Spring中的应用程序上下文可以形成层次结构,其中子上下文可以访问在父上下文中定义的bean。 >

典型的Spring MVC Web应用程序包含两个层次的层次结构:




  • ContextLoaderListener 加载的根web应用程序上下文。
    此上下文的配置位置默认为 applicationContext.xml ,可以使用< context-param> 命名为 contextConfigLocation ,即在 web.xml 的顶层。此上下文通常包含核心应用程序逻辑。


  • DispatcherServlet 加载的Servlet特定上下文。其配置位置默认为< servletname> -servlet.xml ,可以使用< init-param> 命名为 contextConfigLocation ,即在servlet级别。这个上下文通常包含一个Spring MVC相关的东西(控制器等),因为 DispatcherServlet 是Spring MVC的一部分。




后一个上下文是前者的子类。



如果Web应用程序不使用Spring MVC表示框架,它没有 DispatcherServlet 及其上下文。一些非常简单的Spring MVC示例没有 ContextLoaderListener 和根上下文(但是,您需要用于交叉servlet功能的根上下文,例如Spring Security)。



Web应用程序的配置文件默认位于webapp的根文件夹中。但是,它们可以放置在类路径中(即 src / main / webapp ),在这种情况下,它们可以通过 classpath:前缀。如果要在没有servlet容器的集成测试中使用这些文件中的一些,这可能很有用。当你想从一个单独的工件加载一个配置文件时, classpath:前缀可能是有用的,即从 / WEB-INF / lib


I've looked at a bunch of sample projects and I can't seem to tease out a common best practice. I've seen Spring bean config files sometimes go in the src/main/webapp/WEB-INF directory. I've seen this in conjunction with with a Servlet definition in web.xml like this:

<servlet>
    <servlet-name>my-stuff</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/my-stuff-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

But I've also seen bean config files included within web.xml top level -- i.e. outside of a Servlet. What does this mean? Is this for cross-Servlet beans? Sometimes it's in the src/main/webapp/WEB-INF directory and sometimes it's in src/main/resources. Also I've seen other bean config files defined in WAR modules with just about everything in src/main/resources.

I've read and re-read the Spring documentation, but the only convention I found is that by default a Servlet context config file should be in the src/main/webapp/WEB-INF directory named {servlet-name}-servlet.xml.

So what's the best practice and why?

解决方案

Application contexts in Spring can form hierarchies where child context has access to beans defined in parent context.

A typical Spring MVC web application contains a hierarchy with two levels:

  • Root web application context loaded by ContextLoaderListener. Config location of this context is applicationContext.xml by default and can be configured using <context-param> named contextConfigLocation, i.e. at the top level of web.xml. This context usually contains a core application logic.

  • Servlet-specifc context loaded by DispatcherServlet. Its config location is by default <servletname>-servlet.xml and can be configured using <init-param> named contextConfigLocation, i.e. at servlet level. This context usually contains a Spring MVC-related stuff (controllers, etc) since DispatcherServlet is a part of Spring MVC.

The latter context is a child of the former.

If web application doesn't use Spring MVC as a presentation framework, it doesn't have DispatcherServlet and its context. Some extremely simple Spring MVC samples doesn't have ContextLoaderListener and the root context (however, you need root context for cross-servlet functionality such as Spring Security).

Config files of web application are by default located in webapp's root folder. However, they can be placed in the classpath (i.e. in src/main/webapp), in this case they are accessed via classpath: prefix. This may be useful if you are going to use some of these files in integration tests without servlet container. Also classpath: prefix may be useful when you want to load a config file from a separate artifact, i.e. from a jar file in /WEB-INF/lib.

这篇关于Spring bean配置文件在Maven WAR模块中放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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