Configuring Beans and decorating beans based on annotations

Source: Internet
Author: User

1. Component Scan

The spring container is able to automatically scan, detect, and instantiate components with specific annotations from the Classpath (classpath).

2. Specific annotation components

– @Component: Basic annotations that identify a component that is managed by Spring– @Respository: Identify the persistence layer component– @Service: Identify service layer (business layer) components– @Controller: Identify presentation layer Componentsfor scanned components, Spring has a default naming policy: Use unqualified class name, first letter lowercase . You can also identify the name of a component in annotations by using the Value property values 3. How to configure the bean for the annotation component in spring configuration
A, declare <context:component-scan> in the Base-package property, specify the base class package that you want, and the spring container automatically scans the base class package and all classes in its subclass package,    separated by commas when there are multiple packages
b, if you only need to scan the specified class, you can use the Resource-pattern property, such as <context:component-scan base-package= "Com.spring.bean" Reso urce-pattern= "Autowire/*.class"/>
c, <context:include-filter> child nodes need to include the target classes that need to be used in conjunction with
<context:exculde-filter> sub-nodes need to exclude included target classes
<context:include-filter> and <context:exclude-filter> sub-nodes support multiple types of filter expressions:
1<context:component-scan base- Package= "Com.spring.bean.annotation" resource-pattern= "Service/*.class" ></context:component-scan>2<context:component-scan base- Package= "Com.spring.bean.annotation" use-default-filters= "false" >3<context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>4</context:component-scan>5<context:component-scan base- Package= "Com.spring.bean.annotation" use-default-filters= "false" >6<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>7</context:component-scan>

Component Assembly<context:component-scan>  elements are also automatically registered  autowiredannotationbeanpostprocessor instance ,  This instance can be automatically assembled with   @autowired  @Resource   @Inject annotations automatically assemble beans with @ autowired
– Constructors , normal fields ( even non-public ), all methods with parameters can be applied @authwired Annotations –DefaultCases of,all use@Authwiredthe attributes of the annotations need to be set . when Spring cannot find a matching Bean assembly Property , an exception is thrown , and if a property is allowed to not be set , you can set the @ the Required property of the authwired annotation is false –DefaultCases of,WhenIOCThere are multiple types compatible in the container.bean ,  automatic assembly by type will not work .  at this time can be in   @Qualifier   annotations provided  bean . spring @ has been specified to inject  Bean < span data-wiz-span= name of "Data-wiz-span" – @ Authwired  annotations can also be applied to ,  at this time  spring bean  automatic assembly – The@authwired annotation can also be applied on the collection property , when Spring reads the type information of the collection and then automatically assembles all compatible Bean. –@authwired  annotations with in  java.util.map  on ,  map string,  Then  spring  will be automatically assembled with the  map bean,  at this time  Bean   • use @Resource or @Inject Automatic assembly of beans •Spring also supports @Resource and @Inject annotations, which are similar to the functions of the @ autowired annotations •@Resource annotations require a property of a bean name that, if it is empty, automatically takes the variable or method name at the label as the name of the Bean •@Inject and @autowired annotations are beans injected by type matching , but no reqired Property • recommended @autowired annotations  

Configuring Beans and decorating beans based on annotations

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.