Annotation-config, annotation-driven, compont-scan differences

Source: Internet
Author: User

Comprehensive online answers

<ContExT: annotation-config/>

Implicitly register autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor
And equiredannotationbeanpostprocessor.
Before using <context: annotationconfig/> in the configuration file, you must declare the context namespace in the <beans> element.

For example:

If you want to use the @ autowired annotation, you must declare the autowiredannotationbeanpostprocessor bean in the spring container in advance. The traditional statement is as follows:

 
 
  1. <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

To use annotations such as @ resource, @ postconstruct, and @ predestroy, you must declare commonannotationbeanpostprocessor.

To use the @ persistencecontext annotation, you must declare the bean of persistenceannotationbeanpostprocessor.

To use the @ required annotation, you must declare the bean of requiredannotationbeanpostprocessor. Similarly, the traditional statement is as follows:

 
 
  1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

In general, these annotations are often used, especially antowired annotations, during Automatic injection, therefore, if you always need to configure one item in the traditional way, it is cumbersome and unnecessary, so Spring provides us with a simplified configuration method of <context: annotation-config/>, the statement is automatically completed for you.

However, we usually configure the scan package path option when using annotations.

 
 
  1. <context:component-scan base-package=”XX.XX”/>

This configuration item also includes the ability to automatically inject the preceding processor. Therefore, when <context: component-Scan/> is used, <context: annotation-config/> can be removed.

 

<Context: component-Scan/>

The configuration item not only enables scanning of class packages to implement annotation-driven bean definition, the annotation driver automatic injection function is also enabled (that is, autowiredannotationbeanpostprocessor and commonannotationbeanpostprocessor are implicitly registered internally). Therefore, when <context: component-Scan/> is used, unless persistenceannotationbeanpostprocessor and equiredannotationbeanpostprocessor are required, such as JPA
<Context: annotation-config/> removed

 

<MVC: annotation-driven/>
It is equivalent to registering two beans: defaultannotationhandlermapping and annotationmethodhandleradapter, and configuring messageconverter. This solves the prerequisite configuration of @ controller annotation.

<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 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 beansIn the same application context it is definedAnd Declares support for all the general annotations like @ autowired, @ resource, @ required, @ postconstruct 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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.