Automatic bean assembly in xml configuration

Source: Internet
Author: User

In the XML file,
First, let's look at the following code:

<bean id="student" class="com.jeremy.spring.beans.student">    <property name="s_name" value="jeremy"></property>    <property name="age" value="20"></property>    </bean>        <bean id="teacher" class="com.jeremy.spring.beans.teacher">    <property name="tch_name" value="java"></property>    <property name="tch_age" value="20"></property>    <property name="student">            <ref bean="student"/>    </property>        </bean> 

Color enhancement section. When we used to depend on other beans for Bean, we needed to specify the bean for container initialization and loading, but is there a way to automatically load bean dependencies ?? You do not need to specify which bean is used.

The answer is yes:

The Spring IoC container can automatically assemble beans.
You only need to specify the automatic assembly mode in the autowire attribute of <bean>.
Bytype (Automatic Assembly based on types): If the IOC container contains multiple beans of the same type as the target bean. in this case, spring cannot determine which bean is the most suitable for this attribute, so it cannot execute automatic assembly. (for example, if bean1 has an attribute whose type is the object's student type, spring will automatically configure the bean of the object's student type in the XML file after this type is specified. If so, it will be assembled, automatic Assembly cannot be executed if there are no beans of the same type ,)
Byname (automatically assembled by name): the name and attribute name of the target Bean must be set exactly the same (case sensitive)
Constructor (automatically assembled by constructor): This automatic assembly method is complicated when multiple constructor exists in the bean.

Code:

<bean id="student" class="com.jeremy.spring.beans.student">    <property name="s_name" value="tom"></property>    <property name="age" value="20"></property>    </bean>        <bean id="teacher2" class="com.jeremy.spring.beans.teacher" autowire="byName" >    <property name="tch_name" value="java"></property>    <property name="tch_age" value="20"></property>    </bean> 

 

Automatic bean assembly in xml configuration

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.