您的位置:首页 > 编程语言 > Java开发

SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

2014-06-27 07:55 896 查看
在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做。

Spring依赖注入(DI)的三种方式,分别为:

1. Setter方法注入

2. 构造方法注入

3. 接口注入

下面介绍一下这三种依赖注入在Spring中是怎么样实现的。

Setter方法注入

首先我们需要以下几个类:

接口 Logic.java

接口实现类 LogicImpl.java

一个处理类 LoginAction.java

还有一个测试类 TestMain.java

Logic.java如下:

<span style="font-size:18px;"><span style="font-size:18px;">package DI;
//定义接口
public interface Logic {
public String getName();
}
</span></span>


LogicImpl.java如下:
<span style="font-size:18px;"><span style="font-size:18px;">package DI;

public class LogicImpl implements Logic {
//实现类

public String getName() {

return "lishehe";
}

}
</span></span>


LoginAction.java 会根据使用不同的注入方法而稍有不同

Setter方法注入:

<span style="font-size:18px;"><span style="font-size:18px;">package DI;

public class LoginAction {
private Logic logic;

public void execute() {

String name = logic.getName();

System.out.print("My Name Is " + name);

}

/**

* @return the logic

*/

public Logic getLogic() {

return logic;

}

/**

* @param logic

*            the logic to set

*/

public void setLogic(Logic logic) {

this.logic = logic;

}
}
</span></span>


客户端测试类

TestMain.java

<span style="font-size:18px;">package DI;

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

public class TestMain {

/**

* @param args

*/

public static void main(String[] args) {

// 得到ApplicationContext对象

ApplicationContext ctx = new FileSystemXmlApplicationContext(

"applicationContext.xml");

// 得到Bean

LoginAction loginAction = (LoginAction) ctx.getBean("loginAction");

loginAction.execute();

}
}
</span>


定义了一个Logic 类型的变量 logic, 在LoginAction并没有对logic 进行实例化,而只有他对应的setter/getter方法,因为我们这里使用的是Spring的依赖注入的方式

applicationContext.xml配置文件如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="logic" class="DI.LogicImpl"/>

<bean id="loginAction" class="DI.LoginAction">

<property name="logic" ref="logic"></property>

</bean>

</beans>
</span>


运行效果:


构造器注入

顾名思义,构造方法注入,就是我们依靠LoginAction的构造方法来达到DI的目的,如下所示:

<span style="font-size:18px;">package DI;

public class LoginAction {

private Logic logic;

public LoginAction(Logic logic) {

this.logic = logic;

}

public void execute() {

String name = logic.getName();

System.out.print("My Name Is " + name);

}

}
</span>


里我们添加了一个LoginAction的构造方法

applicationContext.xml配置文件如下:

<span style="font-size:18px;">	<bean id="logic" class="DI.LogicImpl"/>

<bean id="loginAction" class="DI.LoginAction">

<constructor-arg index="0" ref="logic"></constructor-arg>

</bean></span>


我们使用constructor-arg来进行配置,
index属性是用来表示构造方法中参数的顺序的,如果有多个参数,则按照顺序,从 0,1...来配置

我们现在可以运行testMain.java了,结果跟使用Setter方法注入完全一样.

效果图



其中需要注意一点有:构造函数有多个参数的话,如:参数1,参数2,而参数2依赖于参数1,这中情况则要注意构造函数的顺序,必须将参数1放在参数2之前。

接口注入

下面继续说说我们不常用到的接口注入,还是以LogicAction为例,我们对他进行了修改,如下所示:

LogicAction.java

<span style="font-size:18px;">package DI;

public class LoginAction {

private Logic logic;

public void execute() {

try {

Object obj = Class.forName("DI.LogicImpl")

.newInstance();

logic = (Logic) obj;

String name = logic.getName();

System.out.print("My Name Is " + name);

} catch (Exception e) {

e.printStackTrace();

}

}

}
</span>


配置文件:

<span style="font-size:18px;"><bean id="logic" class="DI.LogicImpl"/>

<bean id="loginAction" class="DI.LoginAction"></span>
效果图



总结

对于Spring的依赖注入,最重要的就是理解他的,一旦理解了,将会觉得非常的简单。无非就是让容器来给我们实例化那些类,我们要做的就是给容器提供这个接口,这个接口就我们的set方法或者构造函数了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐