Automatic assembly properties for Spring Beans

Source: Internet
Author: User

The automatic assembly properties of the bean simplifies the configuration of the XML file.

The automatic assembly properties of the bean are divided into four types:

1.byName

2.byTyoe

3.constructor

4. AutoDetect

ByName:

It finds the ID of the bean in the configuration file or automatically assembles the bean with the same name as the member attribute in this bean, so no more peoperty tags are added to this bean

Example: There are two classes

Public Class person{

}

Public Class customer{

private person p;

Public Setperson (person PS) {

This.p=ps;

}

}

The traditional XML configuration is

<bean id= "P" class= "com.***. Person "/>

<bean id= "Customer" class= "com.***. Customer ">

<property name= "P" ref= "P" ></property>//The (ref) p is instantiated to the Bean's (name) P property to achieve the purpose of Setperson in the method

</bean>

The automatic assembly method of ByName is

<bean id= "P" class= "com.***. Person "/>

<bean id= "Customer" class= "com.***. Customer "autowrite=" byname "/>//Because instances of id=p classes are aliases exactly and com.***. The attribute p of the customer class has the same member variable name so it is automatically assembled into the customer's bean.

Bytype:

The same way: only he searches for the type of member and the type of a bean in the configuration file is automatically assembled, but one thing to be aware of: if multiple beans of the same type will throw an exception, the processing method is. Set the property of the bean that does not need to be loaded primary= "false" (cancel preferred) or autowriter-candidate= "false" (exclude)

Constructor
Try to find one or more beans in the container that match the constructor parameters of the bean that needs to be automatically assembled, and throw an exception if not found

AutoDetect:
First try to use constructor to move the assembly and then use the Bytype mode.

Default Automatic assembly settings:

If you want to apply the same automatic assembly policy to all the beans it creates to simplify configuration, you can add the Default-autowire property on the root element <beans>

<beans default-autowire= "ByName" > ...................</beans>

Automatic assembly properties for Spring Beans

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.