views:

17

answers:

1

Hello!

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 eleminate in spring 3 config files?

+4  A: 

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

<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, etc), even though support for those is the default behaviour.

My advice is to always declare <context:annotation-config>, but don't bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.

skaffman
should I declare context:annotation-config in both applicationContext and servlet, especially if I use different base packages in component-scan tag?
glaz666
Yes, you'll need `<context:annotation-config>` in each context.
skaffman
@skaffman: `<mvc:annotation-driven>` 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
@axtavt: Good point well made
skaffman