Spring Autowire Automatic Assembly

Source: Internet
Author: User

In spring, 5 automatic assembly mode is supported.
    • no– By default, automatic configuration is manually set by the "ref" property
    • byname– is automatically assembled according to the property name. If the name of a bean is the same as the name of another bean property, it will be self-assembled.
    • The bytype– is automatically assembled by data type. If the data type of a bean is the data type of another Bean property, it is compatible and automatically assembled.
    • constructor– the Bytype mode of the constructor parameter.
    • autodetect– If a default constructor is found, use "Automatic assembly construction"; Otherwise, use automatic assembly by type.

Example:

The Customer and person objects are automatically assembled for demonstration.
 public  class   Customer { private   person person;  public   Customer (person person) { this . Person = person;  public  void   Setperson" { this . Person =//  ... } 
 Public class Person {    //...}

1. auto-wiring ' No ' This is the default mode and you need to connect the bean through the ' ref ' attribute.
class= ' Com.yiibai.common.Customer ' >                  <property name= ' person ' ref= ' person '/>    </bean >    class= "Com.yiibai.common.Person"/>
2. auto-wiring ' ByName '

Auto assemble by attribute name. In this case, because the name of the "person" Bean is the same as the property ("person") name of the "Customer" bean, spring will automatically assemble it through the setter method – "Setperson" “.

class= "Com.yiibai.common.Customer" autowire= "byname"/>    class= "Com.yiibai.common.Person "/>
3. auto-wiring ' Bytype '

The bean is automatically assembled by the data type of the attribute. In this case, because the data type in the "person" Bean is the same as the data type of the "customer" Bean's property (Person object), spring automatically assembles it automatically through the setter method. – "Setperson (person person)".

class class= "Com.yiibai.common.Person"/>
4. Auto-wiring ' constructor '

The bean is automatically assembled by attributes by the data type of the constructor parameter. In this case, because the data type of the "person" Bean is the same as the data type of the constructor parameter of the "Customer" Bean's property (Person object), spring is automatically assembled by constructing the method – "Public customer ( Person) ".

class= "Com.yiibai.common.Customer" autowire= "constructor"/>        class= " Com.yiibai.common.Person "/>
5. auto-wiring ' AutoDetect '

If there is a default construction method, it is automatically assembled by construct, otherwise it is automatically assembled using the Bytype way. For spring2.5, the spring3.0 test does not pass, it is estimated to be obsolete.

Spring Autowire Automatic Assembly

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.