Spring Annotation Mode configuration instructions

Source: Internet
Author: User

1, <context:annotation-config/> and <context:component-scan base-package= "xx.xx"/>

In a host-based configuration spring configuration file, you may see a configuration like <context:annotation-config/> that is registered with the spring container autowiredannotationbeanpostprocessor,commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor and requiredannotationbeanpostprocessor the 4 beanpostprocessor.

Registering these 4 beanpostprocessor functions is for your system to be able to identify the corresponding annotations.

For example:

If you want to use @Autowired annotations, you must declare the autowiredannotationbeanpostprocessor Bean in the Spring container beforehand . The traditional declaration method is as follows

<bean class= "Org.springframework.beans.factory.annotation. Autowiredannotationbeanpostprocessor "/>

If you want to use @ Resource,@ postconstruct,@ Predestroy and other annotations, you must declare commonannotationbeanpostprocessor

If you want to use @PersistenceContext annotations, you must declare the Persistenceannotationbeanpostprocessor Bean.

If you want to use @Required annotations, you must declare the Requiredannotationbeanpostprocessor Bean. Similarly, the traditional way of declaring is as follows:

<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 you always need to follow the traditional way a configuration is a bit cumbersome and unnecessary,

So Spring provides us with a simplified configuration of <context:annotation-config/> to help you complete the declaration automatically.

However, we usually use annotations to configure the scan package path option <context:component-scan base-package= "xx.xx"/>

This configuration item also includes the ability to automatically inject the above processor, so when <context:component-scan/> is used , the <context can be : Annotation-config/> removed.

For example:

<!--<context:annotation-config/> can be removed.
<!--automatically scan assembly--with annotations
<context:component-scan base-package= "Com.aaa.action"/>
<context:component-scan base-package= "Com.aaa.service"/>
<context:component-scan base-package= "Com.aaa.dao"/>

Spring Annotation Mode configuration instructions

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.