Spring bean 配置文件在 Maven WAR 模块中的什么位置? [英] Where do Spring bean configuration files go in a Maven WAR module?

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

问题描述

我查看了大量示例项目,但似乎无法找出常见的最佳实践.我见过 Spring bean 配置文件有时会进入 src/main/webapp/WEB-INF 目录.我已经看到了这个与 web.xml 中的 Servlet 定义的结合,如下所示:

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>

但我也看到了包含在 web.xml 顶级中的 bean 配置文件——即在 Servlet 之外.这是什么意思?这是用于跨 Servlet bean 的吗?有时它在 src/main/webapp/WEB-INF 目录中,有时它在 src/main/resources 中.此外,我还看到了在 WAR 模块中定义的其他 bean 配置文件,其中包含 src/main/resources 中的所有内容.

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.

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

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?

推荐答案

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

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

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

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

  • ContextLoaderListener 加载的根 Web 应用程序上下文.该上下文的配置位置默认为applicationContext.xml,可以使用名为contextConfigLocation进行配置,即在顶部web.xml 的级别.此上下文通常包含核心应用程序逻辑.

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

DispatcherServlet 加载的 Servlet 特定上下文.它的配置位置默认为 -servlet.xml 并且可以使用名为 contextConfigLocation 进行配置,即在 servlet 级别.此上下文通常包含与 Spring MVC 相关的内容(控制器等),因为 DispatcherServlet 是 Spring MVC 的一部分.

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.

如果 Web 应用程序不使用 Spring MVC 作为表示框架,则它没有 DispatcherServlet 及其上下文.一些极其简单的 Spring MVC 示例没有 ContextLoaderListener 和根上下文(但是,跨 servlet 功能(例如 Spring Security)需要根上下文).

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

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

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