Java <context:component-scan /> vs <mvc:annotation-driven> 对@Controller 的Spring 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20551217/
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
Spring support for @Controller given by <context:component-scan /> vs <mvc:annotation-driven>
提问by The Gilbert Arenas Dagger
I've been researching what additional capabilities we have when using the mvc:annotation-driven tag and I'm having a difficult time digesting the results, especially in regards to the @Controller annotation. I know this is very similar to this questionbut please hear me out.
我一直在研究使用 mvc:annotation-driven 标记时我们有哪些附加功能,但我很难消化结果,尤其是在 @Controller 注释方面。我知道这与这个问题非常相似,但请听我说。
According to the Spring docs
The basic purpose of the @Controller annotation is to act as a stereotype for the annotated class, indicating its role. The dispatcher will scan such annotated classes for mapped methods, detecting @RequestMapping annotations (see the next section).
@Controller 注解的基本目的是充当注解类的构造型,表明它的作用。调度程序将扫描这些带注释的类以查找映射方法,检测 @RequestMapping 注释(请参阅下一节)。
The documentation then goes on to show that the context:component-scan tag provides this support. So that's all well and good, but then I was looking at what mvc:annotation-driven gives us, and the aforementioned stackoverflow questionprovides the following answer
然后文档继续显示 context:component-scan 标签提供了这种支持。所以这一切都很好,但后来我在看 mvc:annotation-driven 给我们带来了什么,前面提到的 stackoverflow问题提供了以下答案
mvc:annotation-driven declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declrative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.
mvc:annotation-driven 声明了对注解驱动的 MVC 控制器的显式支持(即 @RequestMapping、@Controller,虽然支持这些是默认行为),以及通过 @Valid 和消息正文编组添加对声明性验证的支持 @RequestBody /响应体。
This seems kind of redundant to me. Maybe I don't get what this explicit support is. Again, referring back to the official Spring documentationwe get the following
这对我来说似乎有点多余。也许我不明白这种明确的支持是什么。再次参考 Spring 官方文档,我们得到以下信息
[mvc:annotation-driven] registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers.
[mvc:annotation-driven] 注册了 Spring MVC 将请求分派到 @Controllers 所需的 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter bean。
That sounds pretty similar to the last example I provided from the docs. If anyone can provide some examples as to what we can do with the @Controller annotation using only the context:component-scan tag, what some of the limitations are, then the additional functionality of what we get when adding the mvc:annotation-driven tag, I think that would be very helpful. Thanks in advance for any support on this.
这听起来与我从文档中提供的最后一个示例非常相似。如果有人可以提供一些示例,说明我们可以仅使用 context:component-scan 标记对 @Controller 批注做什么,有哪些限制,那么我们在添加 mvc:annotation-driven 时获得的附加功能标签,我认为这会很有帮助。预先感谢您对此的任何支持。
采纳答案by M. Deinum
Both elements serve an entirely different purpose.
这两种元素都有完全不同的目的。
<context:component-scan />
is, as the name implies, for component scanning. It by default scans for all beans with the @Component
annotation (or "sub"annotations like @Controller
, @Service
etc.). It will only register instances of those classes in the application context as beans. That is all.
<context:component-scan />
顾名思义,用于组件扫描。它通过为与所有豆类默认扫描@Component
注释(或类似的“亚健康”的注解@Controller
,@Service
等等)。它只会在应用程序上下文中将这些类的实例注册为 bean。就这些。
<mvc:annotation-driven />
is for bootstrapping Spring MVC and it registers, amongst others, a RequestMappingHandlerMapping
and RequestMappingHandlerAdapter
. The first links requests to a certain method (the @RequestMapping
annotation on methods in a @Controller
annotated class). The last knows how to execute methods annotated with @RequestMaping
.
<mvc:annotation-driven />
用于引导 Spring MVC,它注册了 aRequestMappingHandlerMapping
和RequestMappingHandlerAdapter
. 第一个将请求链接到某个方法(@RequestMapping
带@Controller
注释的类中方法的注释)。最后一个知道如何执行用@RequestMaping
.
Now <mvc:annotation-driven />
does nothing for scanning or detecting @Controllers
if there are none in the application context then no request mappings are made. Now you have several ways of registering those beans in the application context and one of them is the aforementioned <context:component-scan />
.
现在<mvc:annotation-driven />
不做任何扫描或检测@Controllers
应用程序上下文中是否没有请求映射。现在您有几种在应用程序上下文中注册这些 bean 的方法,其中之一就是前面提到的<context:component-scan />
.
Basically a @Controller
without <mvc:annotation-driven />
is, well, pretty useless as it does nothing but take up memory. It will not be bound to incoming requests, it just hangs around in the application context. It is just another bean like all other beans and nothing special is being done to it. (Recent, but deprecated, versions of Spring register the DefaultAnnotationHandlerMapping
which processes the @Controller
, this is however deprecated).
基本上 a @Controller
without<mvc:annotation-driven />
是非常无用的,因为它只占用内存。它不会绑定到传入的请求,它只是在应用程序上下文中徘徊。它与所有其他 bean 一样只是另一种 bean,没有对其进行任何特殊处理。(最近但已弃用的 Spring 版本注册了DefaultAnnotationHandlerMapping
处理 的@Controller
,但已弃用)。
回答by DwB
The context:component-scan
element lists a package that Spring should scan for @Controller annotations (in the base-package
attribute).
该context:component-scan
元素列出了 Spring 应该扫描的包以查找 @Controller 注释(在base-package
属性中)。
the mvc:annotation-driven
has no such attribute. This is a convenience element that installs a lot of default MVC elements into the application context. These elements are listed in section 16.14.1 of the Spring framework reference. This element does not appear to scan for @Controller annotations.
在mvc:annotation-driven
没有这样的属性。这是一个方便的元素,可将许多默认 MVC 元素安装到应用程序上下文中。这些元素在Spring 框架参考的16.14.1 节中列出。此元素似乎不会扫描 @Controller 注释。
Contrary to popular belief, there is no dependancy between these elements. An @Controller without mvc:annotation-driven
will function without an issue and handle HTTP requests just fine, as long as you have included context:component-scan
with an appropriate base-package
attribute.
与流行的看法相反,这些元素之间没有依赖关系。没有 @Controllermvc:annotation-driven
将正常运行并且可以很好地处理 HTTP 请求,只要您包含context:component-scan
了适当的base-package
属性。
回答by TheSprinter
Case 1(annotation-driven)
This is Enabling Spring annotations tag. All the annotations such as
@Controller, @Service, @Autowired
etc.. can be used. This doesn't creates a bean, but find the the annotations, and spring creates corresponding bean for that class if found annotation(such as@Controller, @Service, @Autowired
etc..) .Case 2(component-scan)
Spring will scan the package (and subpackages) of the classes specified in declaration and creates bean for it.
案例1(注解驱动)
这是启用 Spring 注释标签。
@Controller, @Service, @Autowired
可以使用所有注释,例如等。这不会创建 bean,而是找到注释,如果找到注释(例如@Controller, @Service, @Autowired
等),spring 会为该类创建相应的 bean 。案例2(组件扫描)
Spring 将扫描声明中指定的类的包(和子包)并为其创建 bean。