Autowire attribute in spring)

Source: Internet
Author: User

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 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. In this way, the configuration file is automatically set according to CN. HH. spring. in the service, setsource finds the bean id = "Source" and automatically configures the bean. If the bean is not found, it is 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. It 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. HereCN. 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 not, an exception is thrown.

4. autodetect:

First try to use constructor for automatic assembly, and then use bytype method.

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.