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?
mvc:annotation-driven is a tag added in Spring 3.0 which does the following:
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.
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...<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
.