Default-autowire attribute of beans

Source: Internet
Author: User

The official definition of automatic assembly is as follows:
The Spring IoC container can automatically assemble (autowire) the association between beans that collaborate with each other. Therefore, if possible

Let spring specify the bean collaborators (other dependent beans) by checking the content in beanfactory ). Because

Autowire can be set for a single bean, so some beans can use autowire, while some beans do not. Autowire

It is convenient to reduce or remove the attribute or constructor parameter settings, so that we can reduce the weight of our configuration file.

In fact, automatic assembly is to reduce the number of <ref = "...">.

Let's start from the requirement. We suppose there are four beans, namely bean2, bean3, bean4, bean5... among them, bean2 contains the following

.. I only release bean2.java.

Package com. Test. model;
Public class bean2 {

Private bean3 bean3;

Private bean4 bean4;
 
Private bean5 bean5;

Public bean3 getbean3 (){
Return bean3;
}

Public void setbean3 (bean3 bean3 ){
This. bean3 = bean3;
}

Public bean4 getbean4 (){
Return bean4;
}

Public void setbean4 (bean4 bean4 ){
This. bean4 = bean4;
}

Public bean5 getbean5 (){
Return bean5;
}

Public void setbean5 (bean5 bean5 ){
This. bean5 = bean5;
}
}
We can see that there is no need for automatic assembly.

<Bean id = "bean2" class = "com. Test. model. bean2">
<Property name = "bean3" ref = "bean3"/>
<Property name = "bean4">
<Ref bean = "bean4"/>
</Property>
<Property name = "bean5" ref = "bean5"/>
</Bean>

Obviously, the following attribute settings are cumbersome. We assume that automatic assembly is used.

<Bean id = "bean2" class = "com. Test. model. bean2"/>

No assembly is required.

Of course, automatic assembly must meet two requirements:

(1) The property name in bean2.java must be the same as the bean ID in applicationcontext. xml... that is

Private bean3 bean3; this bean3 (actually corresponds to the get, Set Method) must be

<Bean id = "bean3" class = "com. Test. model. bean3"
Parent = "abstractbean">
<Property name = "name" value = "Tom"/>
<Property name = "password" value = "123"/>
</Bean> This bean3 is the same. Otherwise, it cannot be automatically assembled.

(2) configure an attribute in the declaration. Default-autowire = "byname" (automatically assemble by name)

The configuration file is.

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
Default-autowire = "byname">

<Bean id = "abstractbean" abstract = "true">
<Property name = "ID">
<Value> 1000 </value>
</Property>
<Property name = "name" value = "Java"/>
</Bean>
<Bean id = "bean2" class = "com. Test. model. bean2"/>

<Bean id = "bean3" class = "com. Test. model. bean3"
Parent = "abstractbean">
<Property name = "name" value = "Tom"/>
<Property name = "password" value = "123"/>
</Bean>

<Bean id = "bean4" class = "com. Test. model. bean4" parent = "abstractbean"/>

<Bean id = "bean5" class = "com. Test. model. bean5">
<Property name = "Age" value = "20"/>
</Bean>

Default-autowire = "X"
X has four options: byname, bytype, constructor, and autodetect.
1. byname:
Service. Java
Public class service
{
Source source;
Public void setsource (source)
{
This. Source = source;
}
}
Applicationcontext. xml
<Beans
...
Default-autowire = "byname">
<Bean id = "Source" class = "cn. HH. Spring. dbcpsource" Scope = "prototype"/>
<Bean id = "service" class = "cn. HH. Spring. Service" Scope = "prototype">
</Bean>
</Beans>
CN. HH. Spring. dbcpsource implements the source interface.
The source attribute is not configured for bean service in XML, but autowire = "byname" is set in beans.

Search for the bean id = "Source" based on setsource in CN. HH. Spring. Service and configure it automatically.

Not assembled.
Note: The name of byname is the XXXX of setxxxx in Java. It has nothing to do with the source spelling in the source set above.

All can be
Public class service
{
Source source1;
Public void setsource (source source1)
{
This. source1 = source1;
}
}
2. bytype:
Service. Java is the same as above
Applicationcontext. xml
<Beans
...
Default-autowire = "bytype">
<Bean id = "dbcpsource" class = "cn. HH. Spring. dbcpsource" Scope = "prototype"/>
<Bean id = "service" class = "cn. HH. Spring. Service" Scope = "prototype">
</Bean>
</Beans>
Setsource is not configured, and autowire is changed to "bytype". The configuration file will find the bean that implements the source interface. Here

CN. HH. Spring. dbcpsource implements the source interface, so it is automatically assembled. If not found, it is not assembled.
If two beans in the same configuration file implement the source interface, an error is returned.
The type here refers to the type of the parameter in setsource (source.
3. constructor:
Try to find one or more beans in the container that are consistent with the constructor parameters of the bean to be automatically assembled. If no constructor is found, an exception is thrown.

.
4. autodetect:
First try to use constructor for automatic assembly, and then use bytype

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.