Spring basics -- automatic assembly of XML-based Bean in Spring Config file, springbean

Source: Internet
Author: User

Spring basics -- automatic assembly of XML-based Bean in Spring Config file, springbean

I. Spring IOC containers support automatic Bean assembly. Automatic Assembly means that you do not need to inject values into Bean attributes through <property> or <constructor-arg>.

Ii. Configuration:

Specify the automatic assembly mode in the autowire attribute of <bean>. The default value is no. You can use the default-autowire attribute of the <beans> root element to change the default value.

Iii. Three automatic assembly modes:

1. byType(Automatic assembly by type): If multiple beans of the same type as the target Bean appear in the IOC container. Spring cannot identify which one to use, which is ambiguous. An exception is reported.

2. byName(Automatic Assembly Based on the name): the attribute name of the current Bean and the id value of the target Bean must be set to the same.

3. construtor (automatically assembled Based on the constructor): not recommended.

Iv. Example

E1: The preceding method is specified through <property>.

<bean class="com.nucsoft.spring.Address" id="address">  <property name="addressName" value="beijing"/></bean><bean class="com.nucsoft.spring.Phone" id="phone" p:phoneNum="123456789"/><bean id="employee" class="com.nucsoft.spring.Employee">  <property name="empName" value="emp01"/>  <property name="phone" ref="phone"/>  <property name="address" ref="address"/></bean>

Output: Employee {empName = 'emp01', address = Address {addressName = 'beijing'}, phone = Phone {phoneNum = '000000 '}}

E2: automatic assembly by name

<bean class="com.nucsoft.spring.Address" id="address">  <property name="addressName" value="beijing"/></bean><bean class="com.nucsoft.spring.Phone" id="phone" p:phoneNum="123456789"/><bean id="employee" class="com.nucsoft.spring.Employee" autowire="byName">  <property name="empName" value="emp01"/></bean>

Output: Employee {empName = 'emp01', address = Address {addressName = 'beijing'}, phone = Phone {phoneNum = '000000 '}}

E3: Automatic Assembly based on types

<bean class="com.nucsoft.spring.Address" id="address">  <property name="addressName" value="beijing"/></bean><bean class="com.nucsoft.spring.Phone" id="phone" p:phoneNum="123456789"/><bean id="employee" class="com.nucsoft.spring.Employee" autowire="byType">  <property name="empName" value="emp01"/></bean>

Output: Employee {empName = 'emp01', address = Address {addressName = 'beijing'}, phone = Phone {phoneNum = '000000 '}}

If multiple objects are defined, an error is reported in the spring config file in idea and a prompt is displayed. In addition, if it is run,Org. springframework. beans. factory. NoUniqueBeanDefinitionExceptionThis exception.

V. Details

1. Automatic Assembly of the <bean> attribute setting autowire attribute will assemble all attributes of the Bean. If you want to assemble only one property, the autowire property is not flexible enough.

2. Although the autowire attribute can be used for automatic assembly, the priority is lower than the property configured through <property>. For example:

<bean class="com.nucsoft.spring.Phone" id="phone02" p:phoneNum="1234567890"/><bean class="com.nucsoft.spring.Address" id="address">  <property name="addressName" value="beijing"/></bean><bean class="com.nucsoft.spring.Phone" id="phone" p:phoneNum="123456789"/><bean id="employee" class="com.nucsoft.spring.Employee" autowire="byName">  <property name="empName" value="emp01"/>  <property name="phone" ref="phone02"/></bean>

3. For autowire attributes, automatic assembly based on type or name cannot be used at the same time.

4. XML-based Bean automatic assembly is rarely used in actual projects, because clear configuration is easier to read.

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.