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():呜呜呜

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值