注入依赖:
依赖:对象之间存在的相互关系
注入依赖:对象通过一些方式形成的相互关系(构造器的参数、工厂方法的参数,或给由构造函数或者工厂方法创建的对象设置属性),相比之前的bean自己来控制本身实例化(在构造器中指定依赖关系或者类似使用服务定位器模式来自助控制依赖关系的注入)容器实现了在创建bean时注入这些依赖关系,控制从根本上发生了倒转,也这里也就是我们所说的IOC(控制翻转)。
应用DI原则很明显的有点就是代码更清晰:bean自己本身不用担心与其他bean的依赖关系,实现更高层次的松耦合易如反掌。DI有两种注入方式,setter注入和构造器注入:
我们通过下边的例子可以看出构造器方法注入会使程序变的臃肿,而且setter方法注入便于以后的修改
class Example { public String e; private String s; public Example(String e, String s) { this.e = e ; this.s = s; } public setE(String e) { this.e = e; } }
上边的例子中,this用来作对象的成员变量的引用,并且跟传入的参数(跟成员变量名字相同)相区别。
不过在java中set方法都是声明为public的,而构造方法在需要的情况下可以声明为private,这种情况下只能通过set方法传值。而且set方法要比较灵活,可以随时调用传值,构造方法就只能创建对象时传值,通过上面的例子不难看出构造方法注入
下面举例说明:
Setter注入:
package springapp.test; /** * @author zhangxuegang * @version 1.0 * @created 2012-10-16 上午09:52:22 */ public class ExampleBean {
// No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public void setYears(int years) { this.years = years; } public void setUltimateAnswer(String ultimateAnswer) { this.ultimateAnswer = ultimateAnswer; } public int getYears() { return years; } public String getUltimateAnswer() { return ultimateAnswer; } }
配置文件配置其属性当然也可以是与其他bean的依赖关系
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="testSpring" class="springapp.test.TestSpring"> </bean> <bean id="exampleBean" class="springapp.test.ExampleBean"> <property name="years" value="20"/> <property name="ultimateAnswer" value="sniper123"/> </bean> </beans>
测试注入依赖是否成功:
package springapp.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 简单测试类 * @author zhangxuegang * @version 1.0 * @created 2012-10-15 下午01:47:18 */ public class TestSpring { public static void main(String[] args) throws Exception { StringBuffer buffer=new StringBuffer(); ApplicationContext ctx = new ClassPathXmlApplicationContext( "file:F:/springProject/springapp/war/WEB-INF/applicationTest.xml"); //第一部分对容器使用的测试 TestSpring tp = (TestSpring) ctx.getBean("testSpring"); //测试通过setter注入依赖 ExampleBean eb = (ExampleBean) ctx.getBean("exampleBean"); System.out.println(eb.getYears()); System.out.println(eb.getUltimateAnswer()); tp.test(); } public void test(){ System.out.println("测试成功"); } }
构造方法注入:
Java代码:
package springapp.test; /** * @author zhangxuegang * @version 1.0 * @created 2012-10-16 上午09:52:22 */ public class ExampleBean { // No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } public int getYears() { return years; } public String getUltimateAnswer() { return ultimateAnswer; } }
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="testSpring" class="springapp.test.TestSpring"> </bean> <bean id="exampleBean" class="springapp.test.ExampleBean"> <constructor-arg type="int" value="7500000" /> <constructor-arg type="java.lang.String" value="42" /> </bean> </beans>
测试注入方法:
package springapp.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 简单测试类 * @author zhangxuegang * @version 1.0 * @created 2012-10-15 下午01:47:18 */ public class TestSpring { public static void main(String[] args) throws Exception { StringBuffer buffer=new StringBuffer(); ApplicationContext ctx = new ClassPathXmlApplicationContext( "file:F:/springProject/springapp/war/WEB-INF/applicationTest.xml"); //第一部分对容器使用的测试 TestSpring tp = (TestSpring) ctx.getBean("testSpring"); //测试通过setter注入依赖 ExampleBean eb = (ExampleBean) ctx.getBean("exampleBean"); System.out.println(eb.getYears()); System.out.println(eb.getUltimateAnswer()); tp.test(); } public void test(){ System.out.println("测试成功"); } }
控制台输出:
7500000 42
测试成功
上边不难看出配置文件中配置参数其实是作为构造器参数传入的,下面讲解一下用静态工厂的方法来替代构造器方法:
关于静态工厂的知识:http://ziming.org/archives/7473.html/2