spring servlet 中的 <mvc:annotation-driven /> 和 <context:annotation-config /> 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3977973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:10:14  来源:igfitidea点击:

What's the difference between <mvc:annotation-driven /> and <context:annotation-config /> in servlet?

springspring-mvcspring-3

提问by glaz666

I am migrating from Spring 2.5 to Spring 3.

我正在从 Spring 2.5 迁移到 Spring 3。

They have introduced <mvc:annotation-driven />which does some black magic. This is expected to be declared in servlet configuration file only.

他们已经介绍了<mvc:annotation-driven />哪些可以做一些黑魔法。这预计仅在 servlet 配置文件中声明。

In Spring 2.5 I have just used <context:annotation-config />and <context:component-scan base='...'/>tags declared both in application-context.xmland dispatcher servlet configuration XML with appropriate base packages to scan.

在 Spring 2.5 中,我刚刚使用了<context:annotation-config /><context:component-scan base='...'/>application-context.xml调度程序 servlet 配置 XML 中声明的标签,并带有要扫描的适当基本包。

So I wonder what is the difference between mvc:annotation-drivenand context:annotation-configtags in servlet config and what can I eliminate in Spring 3 config files?

所以,我不知道是什么样的区别mvc:annotation-driven,并context:annotation-config在servlet配置标签和我有什么可以消除在春季3配置文件?

采纳答案by skaffman

<context:annotation-config>declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.

<context:annotation-config>声明支持诸如@Required@Autowired、等一般注释@PostConstruct

<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 declarative validation via @Validand message body marshalling with @RequestBody/ResponseBody.

<mvc:annotation-driven />声明了对注释驱动的 MVC 控制器的显式支持(即@RequestMapping, @Controller,尽管对这些的支持是默认行为),并通过@Valid使用@RequestBody/ 进行消息正文编组来添加对声明性验证的支持ResponseBody

回答by seanhodges

There is also some more detail on the use of <mvc:annotation-driven />in the Spring docs. In a nutshell, <mvc:annotation-driven />gives you greater control over the inner workings of Spring MVC. You don't need to use it unless you need one or more of the features outlined in the aforementioned section of the docs.

<mvc:annotation-driven />Spring 文档中还有一些关于使用的更多细节。简而言之,<mvc:annotation-driven />让您更好地控制 Spring MVC 的内部工作。除非您需要文档的上述部分中概述的一项或多项功能,否则您不需要使用它。

Also, there are other "annotation-driven" tags available to provide additional functionality in other Spring modules. For example, <transaction:annotation-driven />enables the use of the @Transaction annotation, <task:annotation-driven />is required for @Scheduled et al...

此外,还有其他“注释驱动”标签可用于在其他 Spring 模块中提供附加功能。例如,<transaction:annotation-driven />启用@Transaction 注释,这<task:annotation-driven />是@Scheduled 等人所必需的...

回答by Praveen Kumar K S

mvc:annotation-drivenis a tag added in Spring 3.0 which does the following:

mvc:annotation-driven是 Spring 3.0 中添加的一个标签,它执行以下操作:

  1. Configures the Spring 3 Type ConversionService (alternative to PropertyEditors)
  2. Adds support for formatting Number fields with @NumberFormat
  3. Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  4. Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  5. Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody)
  6. Adds support for reading and writing JSON, if Hymanson is o n the classpath (along the same lines as #5)
  1. 配置 Spring 3 Type ConversionService(替代 PropertyEditors)
  2. 添加对使用 @NumberFormat 格式化数字字段的支持
  3. 添加对使用@DateTimeFormat 格式化日期、日历和 Joda 时间字段的支持(如果 Joda 时间在类路径上)
  4. 添加对使用 @Valid 验证 @Controller 输入的支持,如果 JSR-303 提供程序在类路径上
  5. 添加对读取和写入 XML 的支持,如果 JAXB 在类路径上(使用 @RequestBody/@ResponseBody 进行 HTTP 消息转换)
  6. 添加对读取和写入 JSON 的支持,如果 Hymanson 在类路径上(与 #5 相同)

context:annotation-configLooks for annotations on beans in the same application context it is defined and declares support for all the general annotations like @Autowired, @Resource, @Required, @PostConstruct etc etc.

context:annotation-config在定义它的同一应用程序上下文中查找 bean 上的注释,并声明支持所有通用注释,如 @Autowired、@Resource、@Required、@PostConstruct 等。