[Spring tutorial 8] automatic spring assembly and spring tutorial assembly

Source: Internet
Author: User

[Spring tutorial 8] automatic spring assembly and spring tutorial assembly

1. In spring assembly between us, if a bean uses another bean file, the format should be as follows:

<! -- Main bean --> <bean id = "A" class = "com. test. pro. singer "> <property name =" myb "ref =" B "> </property> </bean> <! -- Assembled bean --> <bean id = "B" class = "com. test. pro. Instrument"> </bean>

That is, when beanA needs to use the accessor to inject B, the above configuration is required.

However, in automatic assembly, if the value of myb in beanA is the same as the id in B, it can be injected by default without any display settings, that is, you only need to use the following format:

<! -- Main bean --> <bean id = "A" class = "com. test. pro. Singer"> <! -- <Property name = "myb" ref = "B"> </property> can be omitted --> </bean> <! -- Assembled bean --> <bean id = "myb" class = "com. test. pro. Instrument"> </bean>

2. Core xml configuration file

<Bean id = "piano" class = "com. test. pro. instrument "> <property name =" name "value =" Piano "> </property> </bean> <bean id =" jack "class =" com. test. pro. singer "autowire =" byName "> </bean>


3. bean class

package com.test.pro;import java.util.List;public class Singer {private Instrument piano;public Instrument getPiano() {return piano;}public void setPiano(Instrument piano) {this.piano = piano;}public void saying(){System.out.println(piano.getName());}}

package com.test.pro;public class Instrument {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}


Output result:


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.