House類:只有一個People屬性,驗證引用的ref引用bean的set方法注入方式spring
package com.zhiwei.autowire; public class House { private People people; public People getPeople() { return people; } public void setPeople(People people){ this.people=people; }[@Override](https://my.oschina.net/u/1162528) public String toString() { return "House [people=" + people + "]"; } }
People類:存在一個name和Dog屬性,主要時驗證普通的set方法的值注入app
package com.zhiwei.autowire; /** * People類:屬性:name,dog */ public class People { private String name; private Dog dog; public String getName() { return name; } public void setName(String name) { System.out.println("People is running setting.......");this.name = name; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Overridepublic String toString() { return "People [name=" + name + ", dog=" + dog + "]"; } }
dog:一個屬性:dogName:測試構造方法屬性值的注入ide
package com.zhiwei.autowire; public class Dog { private String dogName; public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public Dog(String dogName) { System.out.println("Dog is initting by coustructor..........");this.dogName = dogName; } [@Override](https://my.oschina.net/u/1162528) public String toString() { return "Dog [dogName=" + dogName + "]"; } }
Spring的配置文件:applicationContext.xml函數
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- default-autowire="autodetect" 設置默認的裝配方式:default檢測 --> <!-- 構造函數注入dogName屬性 --> <bean id="dog" class="com.zhiwei.autowire.Dog"> <constructor-arg index="0" value="xiaohua"/> </bean> <!-- 經過普通屬性注入name屬性:set byName: people屬性dog,若是IOC容器裏面存在id=dog的對象則自動關聯(注意類型必須匹配) byType:people屬性dog,若是IOC容器存在和dog一致的bean則自動關聯(不容許存在多個目標相同類型的bean) --> <bean id="people" class="com.zhiwei.autowire.People" autowire="byName"> <property name="name" value="squirrel"/> </bean> <!-- 經過set方法注入people屬性 --> <bean id="house" class="com.zhiwei.autowire.House"> <property name="people" ref="people"/> </bean> </beans>
測試類:測試
package com.zhiwei.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring裝配:建立IOC容器中bean的寫做關係的行爲稱爲裝配wiring * bean注入的方式: * 1: property:直接屬性注入(set) * 2:constructor-arg:構造函數注入:index 表示參數的序列號 * 注意:使用ref關聯外部bean的bEAN不能包含構造方法,不然報錯,ref關聯屬性只能經過set注入 * * 構造方法順序:默認構造方法注入---->set屬性注入 * * bean初始化順序:實例化bean按照applicationContext.xml配置順序實例化 * * 裝配類型: * @resource:byName-->byType,匹配不上報錯 * @autowired:byType:匹配不上報錯 * * byName:IOC容器中的bean初始化時未找到初始化對象則根據屬性名和IOC容器中id相同的bean關聯,注意最後是經過構造函數 注入對象 * byType:Bean初始化時未指定默認的屬性值,則更具屬性類型一致的其餘bean關聯 * constructor:將關聯的對象做爲構造函數的參數裝配進當前bean * autodetect:自動檢測當前的byType和constuctor裝配方式,不行則報錯 * default:beans設置的默認裝配方式:default-autowire="autodetect" * no:無默認配置方式 */ public class MainTest { public static void main(String[] args) { //初始化IOC容器ApplicationContext ac= new ClassPathXmlApplicationContext("com/zhiwei/autowire/applicationContext.xml"); House house=(House) ac.getBean("house"); System.out.println("house:"+house); People people=(People) ac.getBean("people"); System.out.println("people:"+people);}}