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():呜呜呜
还没有评论,来说两句吧...