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

Source: Internet
Author: User

<annotaion-driven/> Tags:

The implementation class that corresponds to this tag is Org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

A careful reading of its annotated documentation can clearly see the effect of this class. Parse This document:

This class primarily registers instances of 8 classes:

1.RequestMappingHandlerMapping

2.BeanNameUrlHandlerMapping

3.RequestMappingHandlerAdapter

4.HttpRequestHandlerAdapter

5.SimpleControllerHandlerAdapter

6.ExceptionHandlerExceptionResolver

7.ResponseStatusExceptionResolver

8.DefaultHandlerExceptionResolver

1 is the processing of @requestmapping annotations, 2. Map the name of the Controller class to the request URL. Both 1 and 2 implement the Handlermapping interface, which is used to process request mappings.

3 is the controller class that handles @controller annotations, and 4 is the controller class that handles inheriting Httprequesthandleradapter classes, 5. Handles the controller that inherits the Simplecontrollerhandleradapter class. So these three are for processing requests. Specifically, it is the method that determines which controller is called to handle the current request.

6,7,8 all inherit Abstracthandlerexceptionresolver, this class implements Handlerexceptionresolver, the interface definition: an object implemented by an interface can resolve processor mappings, exceptions thrown during execution, There is also the wrong view.

So the <annotaion-driven/> tag is mainly used to help us deal with the request mapping, decide which controller's method to handle the current request, exception handling.

<context:component-scan/> Tags:

Its implementation class is Org.springframework.context.annotation.ComponentScanBeanDefinitionParser.

Put the mouse on the Context:component-scan to know what the role of the package is used to scan the @repository @Service @Controller annotation class, and then registered to the factory. and Context:component-scan activates @ required. @ resource,@ autowired, @PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit. Makes it possible to use @autowired when the bean is applied.

This article to go straight to the point, explained directly:  
one, <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 advance in the Spring container. &NBSP
2, if you want to use @ Resource, @ postconstruct, @ Predestroy and other annotations, you 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
corresponds to the traditional way of declaring:

<bean class=" Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>  <bean class= " Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor "/> <bean class= " Org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor "/> << Span class= "Hljs-title" >bean class= " Org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor "/>    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

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 @RequestBo dy/@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's defined and declares support for all the general Anno Tations 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, was <task:annotation-driven /> required for @Scheduled et al

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

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.