Spring Bean的自動注入

    Spring的自動裝配可經過<beans/>元素的default-autowire屬性指定,也可經過<bean>元素的autowire屬性指定。自動裝配能夠指定到單獨的Bean,同一個Spring容器中可讓某些Bean使用自動裝配,而另外一些Bean不使用自動裝配。java

    使用autowire屬性配置自動裝配,autowire能夠設置以下值:spring

    1)no:不使用自動裝配。Bean依賴必須經過ref元素定義。也是Spring默認配置。app

    2)byName:根據屬性名自動裝配,BeanFactory查找容器中的所有Bean,找出其中id屬性與屬性同名的Bean來完成注入,若是沒有找到匹配的Bean實列,則Spring不會進行注入。ide

    3)byType:根據屬性類型自動裝配。BeanFactory查找容器中的所有Bean,若是正好有一個與依賴屬性類型相同的Bean,就自動注入這個屬性;若是有多個相同類型的Bean,就會拋出一個異常;若是沒有匹配的Bean,則什麼都不會發生。測試

    4)constructor:與byType相似,區別是用於構造注入的參數。若是BeanFactory沒有符合該Bean的構造器,則會注入異常。this

    5)autodetect:BeanFactory根據Bean內部結構,決定使用constructor或者byType。若是含有一個默認的構造器,就會應用byType。spa

byName規則

    byName規則是指經過名字注入依賴關係,仍是使用「人」與「斧子」的列子,假如Bean Chinese 包含setAxe()方法,而Spring中擁有ID爲「axe」的Bean,這樣Spring容器會將斧子axe注入到人Chinese中。code

<!-- 將 stoneAxe經過byName注入chinese中的axe屬性-->
   <bean id="chinese" class="com.custle.spring.Chinese" autowire="byName"/>  
   <!--  將StoneAxe交給Srping管理 -->
   <bean id="axe" class="com.custle.spring.StoneAxe"/>

在Chinese類中定義setAxe()方法:component

package com.custle.spring;

public class Chinese implements Person {

	private Axe axe;
	
	//構造器注入所須要的斧子
	public Chinese(Axe axe) {
		this.axe = axe;
	}
	
	public Chinese() {
	}
	//注意該set方法後的名字與Bean中定義的ID爲axe相同
	public void setAxe(Axe axe) {
		this.axe = axe;
	}

	@Override
	public void useAxe() {
		System.out.println(axe.chop());
	}

}

這樣就經過byName屬性將石斧注入到Chinese中。接下來看一下註解用法;xml

首先在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"
	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/springcontext.xsd">
	<!-- 掃描使用註釋的包 -->
	<context:component-scan base-package="com.custle.spring"/>
	<!-- 聲明使用byName注入-->
    <bean id="chinese" class="com.custle.spring.Chinese" autowire="byName"/>  
</beans>

在StoneAxe類經過@Component(value="stone")註釋將其交由Spring管理;

@Component(value="stone")
public class StoneAxe implements Axe {

	@Override
	public String chop() {
		return "使用石斧砍材";
	}
		
}

在Chinese中經過@Resource(name="stone")或者@Autowired、@Qualifier("stone")使用;

package com.custle.spring;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Chinese implements Person {
	
//	@Resource(name="stone") 或者直接使用該註釋
	
	@Autowired
	@Qualifier("stone")
	private Axe axe;
	
	static{
		System.out.println("Chinese被實列化");
	}
	
	//構造器注入所須要的斧子
	public Chinese(Axe axe) {
		this.axe = axe;
	}
	
	public Chinese() {
	}
	
	public void setAxe(Axe axe) {
		this.axe = axe;
	}

	@Override
	public void useAxe() {
		System.out.println(axe.chop());
	}

}

測試代碼:

public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-spring.xml");
		Chinese chinese = (Chinese)applicationContext.getBean("chinese");
		chinese.useAxe();
	}

控制檯輸出:

使用石斧砍材

在這裏附帶說一下使用@Resource註釋的裝配順序:

    當@Resource後面沒有任何內容時,默認使用byName的屬性去匹配Bean,找不到Bean時再按照byType去匹配Bean;當指定了裝配類型就按照指定的裝配類型去匹配Bean,若沒有找到匹配的Bean則會產生異常。

@Autowired和@Resource註釋的區別:

    @Autowired默認使用byType方式去裝配Bean,而@Resource是使用byName方式去裝配Bean。@Resource是屬於JAVA的註解,而@Autowired是屬於Spring的註解。

byType規則

    byType規則,指根據類型匹配來注入依賴關係。假如Chinese實列有setAxe(Axe axe)方法,而Spring配置文件中擁有Axe類型的實列,則能夠將Axe類型的實列注入到Chinese實列中。若Spring中不含有Axe類型的實列,也不會發生異常。可是當Spring容器中含有多個Axe類型的實列則會發生異常。

    現將配置類型放置全局中,全部該<beans>下的子元素<bean>都知足該裝配方式:

<?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"
	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/springcontext.xsd"
    default-autowire="byType">
	<!-- 掃描使用註釋的包 -->
	<context:component-scan base-package="com.custle.spring"/>  
</beans>

    因爲使用了byType的配置方式,將StoneAxe交由Spring容器管理時,直接使用@Component便可,無須定義name;在Chinese實列中使用@Resource和@Autowired都可。

    在某些狀況下,能夠限制Bean做爲自動裝配的候選者,則可使用autowire-candidate="false"便可將該Bean限制在自動裝配範圍以外。或者在<beans>元素中使用default-autowire-candidates="false"便可將該<beans>下全部的子元素<bean>限制在自動裝配範圍以外。

相關文章
相關標籤/搜索