由< context:component-scan />给出的@Controller的Spring支持vs< mvc:annotation-driven> [英] Spring support for @Controller given by <context:component-scan /> vs <mvc:annotation-driven>

查看:94
本文介绍了由< context:component-scan />给出的@Controller的Spring支持vs< mvc:annotation-driven>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究使用mvc:annotation-driven标签时我们有哪些额外的功能,我很难消化结果,特别是在@Controller注释方面。我知道这与这个问题非常相似,但请听一下我出去了。

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 question but please hear me out.

根据 Spring docs


@Controller注释的基本目的是采取行动作为注释类的构造型,表明其作用。调度程序将扫描这些带注释的类以查找映射方法,检测@RequestMapping注释(参见下一节)。

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

然后文档继续显示上下文:component-scan标记提供此支持。所以这一切都很好,但后来我看着mvc:annotation-driven给了我们什么,以及前面提到的stackoverflow 问题提供以下答案

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 question provides the following answer


mvc:annotation-driven声明显式支持注释驱动的MVC控制器(即@RequestMapping,@ Controller,虽然支持这些是默认行为),以及通过@Valid添加对解除验证的支持以及使用@RequestBody / ResponseBody添加消息体编组。

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.

这对我来说似乎有点多余。也许我没有得到这种明确的支持。再次,回顾官方的Spring 文档我们得到以下内容

This seems kind of redundant to me. Maybe I don't get what this explicit support is. Again, referring back to the official Spring documentation we get the following


[mvc:annotation-driven]注册DefaultAnnotationHandlerMapping和Spring MVC将请求分派给@Controllers所需的AnnotationMethodHandlerAdapter bean。

[mvc:annotation-driven] registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers.

这听起来与我从文档提供的最后一个例子非常相似。如果有人能够提供一些关于我们如何使用@Controller注释只使用上下文:component-scan标签,一些限制是什么,那么我们在添加mvc:annotation-driven时获得的附加功能的示例标签,我认为这将非常有帮助。提前感谢您对此的任何支持。

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.

推荐答案

这两个元素的用途完全不同。

Both elements serve an entirely different purpose.

< context:component-scan /> 顾名思义就是组件扫描。它默认使用 @Component 注释扫描所有bean(或者sub注释,如 @Controller @Service 等)。它只会将应用程序上下文中这些类的实例注册为bean。这就是全部。

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

< mvc:annotation-driven /> 用于引导Spring MVC并注册其中包括 RequestMappingHandlerMapping RequestMappingHandlerAdapter 。第一个链接请求某个方法( @RequestMapping 注释 @Controller 注释类中的方法)。最后一个知道如何执行用 @RequestMaping 注释的方法。

<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 /> 无法扫描或检测 @Controllers 如果应用程序上下文中没有,则不会进行任何请求映射。现在,您有几种方法可以在应用程序上下文中注册这些bean,其中一种方法是前面提到的< context:component-scan />

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

基本上 @Controller 没有< mvc:annotation-driven /> 很好,没用,因为它只会占用内存。它不会绑定到传入的请求,它只是在应用程序上下文中挂起。它只是像所有其他bean一样的另一个bean,并没有做任何特别的事情。 (最近但已弃用的Spring版本注册了 DefaultAnnotationHandlerMapping ,它处理 @Controller ,但不推荐这样做)。

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

这篇关于由&lt; context:component-scan /&gt;给出的@Controller的Spring支持vs&lt; mvc:annotation-driven&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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