在spring中,能夠不顯示的爲bean初始化屬性,而是經過autowire去自動尋找配置文件中,合適的數據來初始化。具體來看一個例子,學生類中包含一個地址類。java
地址類:
spring
package cn.net.bysoft.lesson2; public class Address { @Override public String toString() { return "Address [name=" + name + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; }
學生類:express
package cn.net.bysoft.lesson2; public class Student { @Override public String toString() { return "Student [name=" + name + ", address=" + address + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } private String name; private Address address; }
下面使用自動裝配來初始化學生類:app
<!-- 自動裝配autowire --> <bean id="address" class="cn.net.bysoft.lesson2.Address" p:name="beijing"></bean> <bean id="student" class="cn.net.bysoft.lesson2.Student" p:name="Lucy" autowire="byType"></bean>
使用autowire=byType來自動選擇合適類型的bean來初始化student,也就是說在student中,有類型是Address的字段,spring自動在配置文件中搜索類型是Address的對象,搜索到後初始化給student類。less
@Test public void testAutowireByType() { Student student = (Student) ctx.getBean("student"); System.out.println(student); /** * output:Student [name=Lucy, address=Address [name=beijing]] * */ }
使用byType有一個須要注意的地方,若是一個配置文件中,有重複的類型,則初始化會報錯,好比在配置文件中有2個address對象:ide
<!-- 自動裝配autowire --> <bean id="address" class="cn.net.bysoft.lesson2.Address" p:name="beijing"></bean> <bean id="address2" class="cn.net.bysoft.lesson2.Address" p:name="shanghai"></bean> <bean id="student" class="cn.net.bysoft.lesson2.Student" p:name="Lucy" autowire="byType"></bean>
@Test public void testAutowireByType() { Student student = (Student) ctx.getBean("student"); System.out.println(student); /** * exception:警告: Exception encountered during context initialization - * cancelling refresh attempt: * org.springframework.beans.factory.UnsatisfiedDependencyException: * Error creating bean with name 'student' defined in class path * resource [applicationContext.xml]: Unsatisfied dependency expressed * through bean property 'address': : No qualifying bean of type * [cn.net.bysoft.lesson2.Address] is defined: expected single matching * bean but found 2: address,address2; nested exception is * org.springframework.beans.factory.NoUniqueBeanDefinitionException: No * qualifying bean of type [cn.net.bysoft.lesson2.Address] is defined: * expected single matching bean but found 2: address,address2 * */ }
這是在初始化會有一個異常,spring告訴咱們找到了兩個bean。分別是address和address2。測試
在這種時候,能夠將autowire="byType"設置成autowire="byName",讓spring經過student的字段名稱進行匹配,也就是說,若是student類中的Address類型的字段叫address的話,會初始化beijing這個address。而若是Student類中的Address類型的字段叫address_new的話,會去配置文件中查找bean的id爲address_new的對象,而後初始化給student類。this
修改一下student類:.net
package cn.net.bysoft.lesson2; public class Student { @Override public String toString() { return "Student [name=" + name + ", address=" + address_new + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress_new() { return address_new; } public void setAddress_new(Address address_new) { this.address_new = address_new; } private String name; private Address address_new; }
在將byType修改成byName,進行測試:code
<!-- 自動裝配autowire --> <bean id="address" class="cn.net.bysoft.lesson2.Address" p:name="beijing"></bean> <bean id="address2" class="cn.net.bysoft.lesson2.Address" p:name="shanghai"></bean> <bean id="address_new" class="cn.net.bysoft.lesson2.Address" p:name="guangzhou"></bean> <bean id="student" class="cn.net.bysoft.lesson2.Student" p:name="Lucy" autowire="byName"></bean>
@Test public void testAutowireByName() { Student student = (Student) ctx.getBean("student"); System.out.println(student); /** * output:Student [name=Lucy, address=Address [name=guangzhou]] * */ }