[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: