Annotation-config, Annotation-driven, Compont-scan difference

Source: Internet
Author: User

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:

  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 was on the classpath
  4. Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider are on the classpath
  5. Adds support for reading and writing XML, if JAXB was on the classpath (HTTP message conversion with @Reque stbody/@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 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

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.