Spring IOC&DI之Setter注入

 

上一節總結了依賴注入中的構造器注入方式,本節總結基於Setter方式的注入及xml文件的配置方式。

 

還是根據所依賴對象的類型分爲:

(1)基本類型:如int型、String型等。

(2)自定義類型:自定義的類型,也就是封裝成了一個單獨的bean。

(3)容器類型:如List、Set、Map等

(4)空類型

 

使用Setter方法注入必須要遵循javabean規範,類內部使用setter/getter方法來設置或者獲取屬性值。

而且在調用者類內必須要有默認構造函數。如果已經定義了含參構造函數,則必須顯式的定義無參構造函數。

因爲setter注入方式即調用類中的setter方法將其他屬性注入,既然要調用setter方法則需要有對象,spring框架內部就是使用反射原理+類名+默認構造函數先創建一個對象,然後調用setter方法。

 

具體定義如下所示:

public class MyService{
	
	private MyDao myDao; //引用其他bean
	private Description des; //引用其它bean
	
	private List<String> list = new ArrayList<String>(); //list容器 
	private Set<String> set = new HashSet<String>(); //set容器
          //map容器
	private Map<String, Integer> maps = new HashMap<String, Integer>();

         //setter & getter方法
	public void setMyDao(MyDao myDao){
		this.myDao = myDao;
	}
	
	public MyDao getMyDao(){
		return myDao;
	}

	public void setDes(Description des){
		this.des = des;
	}
	
	public Description getDes(){
		return des;
	}
	
	public void setList(List<String> list){
		this.list = list;
	}
	
	public List<String> getList(){
		return list;
	}
	
	public void setSet(Set<String> set){
		this.set = set;
	}
	
	public Set<String> getSet(){
		return set;
	}
	
	public void setMaps(Map<String, Integer> maps){
		this.maps = maps;
	}
	
	public Map<String, Integer> getMaps(){
		return maps;
	}
	
	public void addUser(){
		myDao.addUser();
		des.descript();
	}
	
	public void print(){
		//打印list中的內容
		for(String str : list){
			System.out.println(str);
		}
		
		for(String str2 : set){
			System.out.println(str2);
		}
		Iterator<Map.Entry<String, Integer>> it = maps.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String, Integer> entry = it.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
	}
}

 

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-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
			default-lazy-init="true">
			<!--對所有bean進行延遲初始化-->
			<!--
			<context:component-scan base-package="com.springframework.ioc"/>
			-->
			<!-- beans declare go here -->
			<bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl">
			<!--調換構造函數順序-->
				<constructor-arg  index="1" value="1"/>
				<constructor-arg  index="0" value="mysql"/>
			</bean>
			<bean id="oracle" class="com.springframework.ioc.OracleDaoImpl">
				<property name="name" value="oracle"/>
			</bean>
			
			<bean id="description" class="com.springframework.ioc.Description"/>
			<!-- 通過setter方法注入-->
			
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<property name="myDao" ref = "mysql"/>
				<property name="des" ref = "description"/>
				<property name="list">
					<list>
						<value>xiaoming</value>
						<value>xiaohong</value>
						<value>xiaolin</value>
					</list>
				</property>
				
				<property name="set">
					<set>
						<value>jin</value>
						<value>yin</value>
						<value>tong</value>
					</set>
				</property>
				
				<property name="maps">
					<map>
						<entry key="wang" value="1"/>
						<entry key="li" value="2"/>
						<entry key="hu" value="3"/>
					</map>
				</property>
			</bean>
			
	</beans>

 總的來說就是使用

<property name=" xxx" ref = "xxx "/>

 或者是

<property name="xxx" value = "xxx"/>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章