"Spring Tutorial Eight" Spring automatic assembly

Source: Internet
Author: User

1. In the spring assembly between us, if one bean uses another bean file, the format should look like this:

<!--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 inject B with an accessor, the above configuration is required.

However, in automatic assembly, if the attribute value in Beana is the same as the ID in B myb, it can be injected by default, without displaying the settings, that is, just use the following format:

<!--main Bean--><bean id= "A" class= "Com.test.pro.Singer" ><!--<property name= "Myb" ref= "B" ></ Property> This sentence 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:


"Spring Tutorial Eight" Spring automatic assembly

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.