1. 程式人生 > >Spring 依賴注入四種方式

Spring 依賴注入四種方式

紙上得來終覺淺

1.構造器注入

AnimalDao:

package com.roadArchitectWeb.dao;
public interface AnimalDao {
	/*所有動物有一個說話的方法*/
	public void say();
}
CatDaoImpl:
package com.roadArchitectWeb.dao;
public class CatDaoImpl implements AnimalDao{
	/*這個時候並沒有值,會有xml檔案注入*/
	private String says;
	public void say(){
		System.out.println("CatDaoImpl.say():"+says);
	}
	/*帶引數,方便利用構造器進行注入*/
	public CatDaoImpl(String says){
		this.says=says;
	}
}
AnimalMain:
package com.roadArchitectWeb.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class AnimalSay1{
	private AnimalDao animalDao;
	public void setAnimalDao(AnimalDao animalDao) {
		this.animalDao = animalDao;
	}
	public void Say(){
		animalDao.say();
	}
	public AnimalSay1(AnimalDao animalDao){
		this.animalDao=animalDao;
	}
}
public class AnimalMain {
	public static void main(String[] args) {
		/*建立Spring的IOX容器物件*/
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*從IOC容器中獲取Bean例項*/
		AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
		/*呼叫其中的say方法*/
		animalSay1.Say();
	}
}
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
<constructor-arg value="喵喵喵"></constructor-arg>
</bean>
<bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
<constructor-arg ref="CatDaoImpl"></constructor-arg>
</bean>
</beans>
輸出結果:

CatDaoImpl.say():喵喵喵

所謂構造器注入,即類中有一個構造方法,同時在配置檔案中利用該構造方法進行注入,上述例子用了兩次構造器注入。

2.setter方法注入

上一篇文章是setter方法的具體應用。

3.靜態工廠

既然是靜態工廠方式注入,首先新建一個工廠用於獲取CatDaoImpl例項:

package com.roadArchitectWeb.dao;

public class DaoFactory {
	/*靜態工廠方法*/
	public static AnimalDao getStaticInstance(String says){
		return new CatDaoImpl(says);
	}
}
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
<constructor-arg value="喵喵喵"></constructor-arg>
</bean>
<bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
<constructor-arg ref="CatDaoImpl"></constructor-arg>
</bean>
<bean id="DaoFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
<constructor-arg value="哇哇哇"></constructor-arg>
</bean>
</beans>
AnimalMain修改為:
package com.roadArchitectWeb.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class AnimalSay1{
	private AnimalDao animalDao;
	public void setAnimalDao(AnimalDao animalDao) {
		this.animalDao = animalDao;
	}
	public void Say(){
		animalDao.say();
	}
	public AnimalSay1(AnimalDao animalDao){
		this.animalDao=animalDao;
	}
}
public class AnimalMain {
	public static void main(String[] args) {
		/*建立Spring的IOX容器物件*/
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*從IOC容器中獲取Bean例項*/
		AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
		/*呼叫其中的say方法*/
		animalSay1.Say();
		
		AnimalDao animalDao = (AnimalDao)ctx.getBean("DaoFactory");
		animalDao.say();
	}
}
結果為:

CatDaoImpl.say():喵喵喵
CatDaoImpl.say():哇哇哇

所謂靜態工廠,不同於建構函式注入和setter方法注入,它通過factory-method指定方法,從而獲得例項的引用。

4.例項工廠

同靜態工廠相同,只是新增一個工廠DaoInstanceFactory:

package com.roadArchitectWeb.dao;

public class DaoInstanceFactory {
	/*例項工廠方法*/
	public  AnimalDao getStaticInstance(String says){
		return new CatDaoImpl(says);
	}
}
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
<constructor-arg value="喵喵喵"></constructor-arg>
</bean>
<bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
<constructor-arg ref="CatDaoImpl"></constructor-arg>
</bean>
<bean id="DaoFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
<constructor-arg value="哇哇哇"></constructor-arg>
</bean>
<bean id="DaoInstanceFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
<constructor-arg value="嗚嗚嗚"></constructor-arg>
</bean>
</beans>
AnimalMain修改為:
package com.roadArchitectWeb.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class AnimalSay1{
	private AnimalDao animalDao;
	public void setAnimalDao(AnimalDao animalDao) {
		this.animalDao = animalDao;
	}
	public void Say(){
		animalDao.say();
	}
	public AnimalSay1(AnimalDao animalDao){
		this.animalDao=animalDao;
	}
}
public class AnimalMain {
	public static void main(String[] args) {
		/*建立Spring的IOX容器物件*/
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*從IOC容器中獲取Bean例項*/
		AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
		/*呼叫其中的say方法*/
		animalSay1.Say();
		
		AnimalDao animalDao = (AnimalDao)ctx.getBean("DaoFactory");
		animalDao.say();
		
		AnimalDao animalDao2 = (AnimalDao)ctx.getBean("DaoInstanceFactory");
		animalDao2.say();
	}
}
結果為:

CatDaoImpl.say():喵喵喵
CatDaoImpl.say():哇哇哇
CatDaoImpl.say():嗚嗚嗚