Spring IOC&DI的應用之構造函數注入

 

前文已經介紹Spring IOC&DI主要解決了對象和對象之間的耦合問題,將每一個對象作爲bean交給Spring容器來管理。本文主要總結Spring IOC&DI的具體應用,包括其xml配置文件寫法、依賴注入方式、bean獲取方式等。

既然是解決對象和對象之間的耦合,那根據所依賴對象的類型可以分爲:

(1)基本類型對象:所依賴對象爲基本類型對象。如:int、String等

(2)自定義類型:所依賴對象爲其他的自定義的類類型。

(3)集合類型:所依賴對象爲集合類型,如List、Set、Map、Property等

(4)null 空值

 

同時根據依賴注入的方式分爲兩種方式:構造器注入和setter注入(接口注入已基本被廢棄)

還是以上一節的例子。

1、構造器注入方式

(1) 基本類型對象注入

假設在dao的實現類中有一個屬性用來記錄具體的數據庫的名字,則定義如下:

 

package com.springframework.ioc;

public class MysqlDaoImpl implements MyDao{
	private String name;
        public MysqlDaoImpl(String name){
		this.name = name;
	}
	@Override
	public void addUser(){
		System.out.println("this is " + name + " add user...");
	}
}

 

需要將name通過構造函數形式注入進來。

 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">
			<!-- beans declare go here -->
			<bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl">
				<constructor-arg  value="mysql"/>
			</bean>
			<!-- 通過含參構造器注入 -->
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg ref = "mysql"/>
			</bean>
</beans>

 如果構造函數中有兩個參數呢,假設現在又加了一個id的參數。

 

package com.springframework.ioc;

public class MysqlDaoImpl implements MyDao{
	private String name;
        private int id;
        public MysqlDaoImpl(String name, int id){
		this.name = name;
                this.id = id;
	}
	@Override
	public void addUser(){
		System.out.println("this is " + name "id is " + id  + " add user...");
	}
}

這個時候可以繼續以上面的方式配置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">
			<!-- beans declare go here -->
			<bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl">
				<constructor-arg  value="mysql"/>
				<constructor-arg  value="1"/>
			</bean>
			<!-- 通過含參構造器注入 -->
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg ref = "mysql"/>
			</bean>
	</beans>

 會輸出正確的結果,

this is mysql id is 1 add user...

 但是如果我調換一下順序,變成這樣:

 

<?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">
			<!-- beans declare go here -->
			<bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl">

				<constructor-arg  value="1"/>
				<constructor-arg  value="mysql"/>

			</bean>
			<!-- 通過含參構造器注入 -->
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg ref = "mysql"/>
			</bean>
	</beans>

此時因爲類型不匹配會報錯,但是如果我兩個參數都是String類型的,就會傻傻分不清楚了,所以,爲了能夠分清楚,一般會如下設置,指定參數的index:

 

<?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">
			<!-- 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 = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg ref = "mysql"/>
			</bean>
	</beans>

 這樣就可以清楚的知道注入的是哪一個參數。

(2) 自定義類型

自定義類型即所依賴的對象爲自定義的類,比如在上一條中介紹的MyService類依賴MysqlDaoImpl類,在xml配置文件中配置如下:

			</bean>
			<!-- 通過含參構造器注入 -->
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg ref = "mysql"/>
			</bean>

如果有兩個參數都是自定義類型,則可以按照以上針對基本類型的做法,設置構造器的index屬性。如下:

<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg index="0" ref = "mysql"/>
				<constructor-arg index="1" ref = "description"/>
			</bean>

 其中description是另外一個引用的bean的id。

 (3)集合類型  

總體概括有四種集合類型:List、Set、Map、Props。

集合中可以存放基本類型,也可以存放自定義類型。

存放基本類型的xml配置如下,分別有List、Set和Map類型,其中List、Set類型容器中存放的是String類型數據,Map類型容器中鍵值爲String類型,值爲Integer類型數據:

<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg index="0" ref = "mysql"/>
				<constructor-arg index="1" ref = "description"/>
				<constructor-arg  index="2">
					<list>
						<value>xiaoming</value>
						<value>xiaohong</value>
						<value>xiaolin</value>
					</list>
				</constructor-arg>
				<constructor-arg  index="3">
					<set>
						<value>jin</value>
						<value>yin</value>
						<value>yin</value>
					</set>
				</constructor-arg>
				<constructor-arg  index="4">
					<map>
						<entry key="wang" value="1"/>
						<entry key="li" value="2"/>
						<entry key="hu" value="3"/> 
					</map>
				</constructor-arg>
			</bean>

 Props與Map類型的容器差不多,只是Props只能存放String類型的鍵和值。

如果容器中存放的是自定義類型,xml文件中的配置如下:

<bean id = "myservice" class="com.springframework.ioc.MyService">
				<constructor-arg index="0" ref = "mysql"/>
				<constructor-arg index="1" ref = "description"/>
				<constructor-arg  index="2">
					<list>
						<ref bean="bean1">
						<ref bean="bean2">
						<ref bean="bean3">
					</list>
				</constructor-arg>
				<constructor-arg  index="3">
					<set>
                                              <ref bean="bean1">
					      <ref bean="bean2">
					      <ref bean="bean3">
					</set>
				</constructor-arg>
				<constructor-arg  index="4">
					<map>
						<entry key="wang" value-ref="bean7"/>
						<entry key-ref="bean10" value-ref="bean8"/>
						<entry key="hu" value-ref="bean9"/> 
					</map>
				</constructor-arg>
			</bean>

 (4)null空值

如果想要給某一個類型賦空值,xml配置文件配置如下:

<constructor-arg  index="0">

      <null/>
</constructor-arg>

 

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