157

I am migrating from Spring 2.5 to Spring 3.

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

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

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
glaz666
  • 8,707
  • 19
  • 56
  • 75

3 Answers3

152

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

<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 @Valid and message body marshalling with @RequestBody/ResponseBody.

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 3
    should I declare context:annotation-config in both applicationContext and servlet, especially if I use different base packages in component-scan tag? – glaz666 Oct 20 '10 at 13:50
  • 2
    Yes, you'll need `` in each context. – skaffman Oct 20 '10 at 13:53
  • 61
    @skaffman: `` is not pointless, it's just improperly named. Actually it configures support for new Spring MVC features such as declarative validation with `@Valid`, HTTP message conversion with `@RequestBody`/`@ResponseBody`, new field conversion architecture, etc. – axtavt Oct 20 '10 at 14:35
  • @axtavt, so I should define the `` ? – Rihards Apr 01 '11 at 11:23
  • 5
    @Richards: Usually yes, see http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/ – axtavt Apr 01 '11 at 11:28
  • 7
    This answer is plain wrong, as mvc:annotation-driven is crucial for conversions and validation. The only correct and worthwhile thing here is comment from @axtavt. – Konrad Garus Jul 20 '12 at 08:51
  • 1
    Good rewrite of the answer, I now consider this accurate. – seanhodges May 29 '13 at 09:41
  • 1
    without annotation-driven atRequestMapping atController will be found but atPathVariable will not be found – Junchen Liu Dec 27 '15 at 22:28
36

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.

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

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • I'd like to see a set of settings in xml which are similar to this tag. Once I have came across with that example long time ago but it was incomplete, AFAIR. – glaz666 Dec 04 '12 at 09:27
7

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

  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 Jackson is o n the classpath (along the same lines as #5)

context:annotation-config Looks 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.

Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
  • 2
    Source and more details on mvc:annotation-driver - https://spring.io/blog/2009/12/21/mvc-simplifications-in-spring-3-0/ – Aniket Thakur Jan 14 '17 at 05:26
  • So you mean to say, If I need `@Autowired, @Resource, @Required, @PostConstruct` AND `@RequestBody, @ResponseBody` - all these annotations then I will have to add both these `mvc:annotation-driven` `context:annotation-config` tags in my config? – R Dhaval Oct 23 '18 at 05:40