Spring 中的核心思為Ioc 與 DI 而DI 則是去實現 Ioc 的一個方式,而@Autowired就是Spring 2.5 後提供的一種方式,它可以對class成員變量、方法及構造函數進行標註,完成自動裝配的工作
一 . @Autowired可使用的地方
1.class 的 setter 方法上面
|
public class User(){ public string name ; @Autowired public void setName(){...} /* spring建立User的bean的時候,就會自動尋找匹配的參數注入到該bean當中 */ } |
2.class 的 建構函式上
|
public class User(){ @Autowired public User(){...} } |
3.成員變量上
|
public class User(){ @Autowired public UserDao userDao ; /* 此作法則可省略UserDao setter 的方法 */ } |
二. @Autowired的實作範例
首先我們先建立一個 Family.java 及 People.java
|
public class Family { private People people; public void getPeople() { return people } public void setPeople(String people) { this.people= people; } } |
|
public class People { private String name; public void getName() { System.out.println(name); } public void setName(String name) { this.people= name; } } |
接著在Spring 的 xml 中加入 People<bean>配置People的value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.springmvc.demo" /> <bean id="family" class="com.springmvc.demo.Family"> </bean> <bean id="people" class="com.springmvc.demo.People"> <property name="name" value="James" /> </bean> </beans> |
接著我們在 Family.java 加上@Autowired
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class Family { @Autowired private People people; public void getPeople() { return people; } public void setPeople(String people) { this.people= people; } } public class MyWeb { @Autowired private WebApplicationContext context; @Autowired private Family family; public static void main(String[] args) { family.getPeople().getName(); } } |
如此一來輸出的結果就會是我們在 xml 中配置 People >> name 的值 ( James )
我們只要將想賦予的值定義於xml中,透過@Autowired即可以直接將直注入,而不
須透過程式碼呼叫setter的方法
三. 當被配置的 bean 數量不為1 時,出現BeanCreationException,以下為解決方式
1.當被配置的 bean 數量不為0 時
我們將xml 中的 Family 註解掉,在執行程式時就會出現BeanCreationException
|
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.springmvc.demo" /> <!--bean id="family" class="com.springmvc.demo.Family"></bean--> <bean id="people" class="com.springmvc.demo.People"> <property name="name" value="James" /> </bean> </beans> |
所以我們可以在@Autowired加上 (required=false),意思就是說在注入時Family不是找不到匹配Bean時也不出Exception
|
public class MyWeb { @Autowired(reequired=false) private WebApplicationContext context; @Autowired private Family family; public static void main(String[] args) { family.getPeople().getName(); } } |
2.當被配置的 bean 數量大於1時,在執行程式時也會出現BeanCreationException
我們在xml 中再加入一個Family的bean,在執行程式時也會出現BeanCreationException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.springmvc.demo" /> <bean id="family" class="com.springmvc.demo.Family"></bean> <bean id="family2" class="com.springmvc.demo.Family"></bean> <bean id="people" class="com.springmvc.demo.People"> <property name="name" value="James" /> </bean> </beans> |
我們配置了兩個Family類型的bean,所以當我們對Family類型注入時,spring無法判斷要用哪一個,我們需要透過@Qualifier指定我們要注入的bean名稱,就可以解決此問題,而@Qualifier亦只能配合@Autowired使用
|
public class MyWeb { @Autowired @Qualifier("family") private WebApplicationContext context; @Autowired private Family family; public static void main(String[] args) { family.getPeople().getName(); } } |