Spring3-spring Automatic Assembly Bean

Source: Internet
Author: User

spring3-spring Automatic Assembly Bean

Spring auto-wiring beans--spring Automatic Assembly Bean

The so-called automatic assembly is the injection of a bean into the property of another bean, similar to the following:

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "ByName"/>

Spring supports 5 automatic assembly modes, as follows:

no--By default, it is not automatically assembled and manually set by the "ref" attribute.

buname--is automatically assembled according to the name of the property, and if the name of a bean is the same as the name of the property in another bean, the bean is automatically assembled into the property.

bytype--is automatically assembled according to the property's data type (type) and automatically assembled if the data type of one bean is compatible with the data type of the property in another bean.

constructor--Automatic assembly of Bytype mode according to the data type of the constructor parameter.

autodetect--If a default constructor is found, use constructor mode, otherwise, use Bytype mode.

The following example shows the automatic assembly

Customer.java as follows:

Package Com.lei.common; public class Customer {    private person person;     Public Customer {        This.person = person;    }     public void Setperson (person person) {        This.person = person;    }    //...}

Person.java as follows:

Package Com.lei.common; public class Person {    //...}

1. auto-wiring ' No '

By default, the bean needs to be assembled through ' ref ', as follows:

<bean id= "Customer" class= "Com.lei.common.Customer" >    <property name= "person" ref= "person"/></ bean> <bean id= "person" class= "Com.lei.common.Person"/>

2. auto-wiring ' ByName '

The bean is assembled according to the name of the property, and in this case, the customer sets the autowire="ByName", and spring automatically looks for the same bean as the property name "person", when found, It is injected into the property by calling Setperson (person person).

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "byname"/><bean id= "person" class= " Com.lei.common.Person "/>

If the corresponding bean configuration is not found according to the property name, the following

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "byname"/><bean id= "Person_another" class= " Com.lei.common.Person "/>

Customer in the property name is person, but the configuration file can not find the person, only person_another, then the assembly fails, after running, the customer person= Null.

3. Auto-wiring ' Bytype

Automatically assembled according to the data type of the attribute property, in which case the customer sets the autowire="Bytype", and spring will always look for the same bean as the property type, and when found, by calling Setperson ( Person person) to inject it.

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "Bytype"/><bean id= "person" class= " Com.lei.common.Person "/>

What if there are two beans of the same type in the configuration file? As follows:

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "Bytype"/><bean id= "person" class= " Com.lei.common.Person "/><bean id=" Person_another "class=" Com.lei.common.Person "/>

Once configured as above, two beans of the same data type are configured and will throw unsatisfieddependencyexception exceptions, see the following:

So, once you have selected the ' Bytype ' type of automatic assembly, make sure that each data type in your profile defines a unique bean.

4. Auto-wiring ' constructor '

In this case, spring looks for the same bean as the parameter data type and injects it through the constructor public Customer (the person person).

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "constructor"/><bean id= "person" class= " Com.lei.common.Person "/>

5. auto-wiring ' AutoDetect '

In this case, spring will first look for a default constructor in the customer and, if there is a "constructor" equivalent to the top, inject it with the constructor, otherwise, it is injected with the ' bytype ' method, so in this case, by calling public the Customer (person person) injects it.

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "AutoDetect"/><bean id= "person" class= " Com.lei.common.Person "/>

Attention:

It is a good way to use Autowire with Dependency-check in a project to ensure that attributes are always successfully injected.

<bean id= "Customer" class= "Com.lei.common.Customer" autowire= "AutoDetect" dependency-check= "Objects"            /> <bean id= "Person" class= "Com.lei.common.Person"/>

In the end, I think that the automatic assembly, while making development faster, will take more effort to maintain because it increases the complexity of the configuration file, and you don't even know which Bean will be automatically injected into another bean. I prefer to write configuration files for manual assembly.

Spring3-spring Automatic Assembly Bean

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.