This article is straight to the point, explained directly separately:
First, <context:annotation-config/>
Implicitly registers the Autowiredannotationbeanpostprocessor, Commonannotationbeanpostprocessor, The 4 beanpostprocessor of Persistenceannotationbeanpostprocessor and Equiredannotationbeanpostprocessor
Make an explanation of this result class:
1. If you want to use @autowired annotations, you must declare the Autowiredannotationbeanpostprocessor Bean in the Spring container beforehand.
2, if you want to use @ Resource, @ postconstruct, @ Predestroy and other annotations must declare commonannotationbeanpostprocessor.
3. If you want to use @persistencecontext annotations, you must declare the Persistenceannotationbeanpostprocessor bean.
4. If you want to use @Required annotations, you must declare the Requiredannotationbeanpostprocessor bean.
The respective traditional declarations are as follows:
<bean class = " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean class = "org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> Span class= "Hljs-tag" ><bean class = "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean class = "Org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor "/>
Generally speaking, these annotations we are more commonly used, especially antowired annotations, in the automatic injection is often used, so if it is always necessary to follow the traditional way a configuration is a bit cumbersome and unnecessary, so spring provides us with a <context:annotation-config>
simplified configuration method, Automatically help you complete the declaration.
<context:annotation-config/>
The Spring container is implicitly registered with the Autowiredannotationbeanpostprocessor, Commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor and Equiredannotationbeanpostprocessor the 4 beanpostprocessor.
Second, <context:component-scan/>
<context:component-scan/>
configuration Items Not only enable the ability to scan class packages to implement annotation-driven Bean definitions, but also enable annotation-driven auto-injection (that is, implicitly, internally registered Autowiredannotationbeanpostprocessor and Commonannotationbeanpostprocessor), so when used <context:component-scan base-package="xxx.xxx.xxx"/>
, it can be <context:annotation-config/>
removed.
By default, beans defined by @Component are singleton, and if you need to use a different scope of beans, you can achieve the goal by @Scope annotations, such as @Scope ("prototype"), so that when the Spring container is When the boss Bean is taken, each return is a new instance.
Third, <mvc:annotation-driven/>
Equivalent to registering the defaultannotationhandlermapping and annotationmethodhandleradapter two beans, configuring some messageconverter. This resolves the use premise configuration of the @controller annotations.
The official documentation is also described in the following:
<mvc:annotation-driven/>
is a tagged added in Spring 3.0 which does the following:
- Configures the Spring 3 Type conversionservice (alternative to Propertyeditors)
- Adds support for formatting number fields with @NumberFormat
- Adds support for formatting Date, Calendar, and Joda time fields with @DateTimeFormat, if Joda time was on the classpath
- Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider are on the classpath
- Adds support for reading and writing XML, if JAXB was on the classpath (HTTP message conversion with @Reque stbody/@ResponseBody)
-
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 Defin Ed and declares support for all the annotations like @Autowired, @Resource, @Required, @PostConstruct etc etc
<context:annotation-config>
does not search for @Component, @Controller, etc.
< Context:component-scan>
DOES Search for those @Component annotations, as well as the annotations that < Context:annotation-config/>
does.
There is 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, & Lt;task:annotation-driven/>
is required for @Scheduled et al
Annotation-config, Annotation-driven, compont-scan differences