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