SSH深度历险(八) 剖析SSH核心原理+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方法或者构造函数了。
SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式的更多相关文章
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
于java发育.一类程序猿必须依靠类的其他方法,它是通常new依赖类的方法,然后调用类的实例,这样的发展问题new良好的班统一管理的例子.spring提出了依赖注入的思想,即依赖类不由程 ...
- SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)
接上一篇 SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP,本篇我们主要是来学习使用配置XML实现AOP 本文采用强制的CGLB代理方式 Security ...
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- 30个类手写Spring核心原理之依赖注入功能(3)
本文节选自<Spring 5核心原理> 在之前的源码分析中我们已经了解到,依赖注入(DI)的入口是getBean()方法,前面的IoC手写部分基本流程已通.先在GPApplicationC ...
- java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- (转)编码剖析Spring依赖注入的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...
- 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型(四)
上篇博文,我们介绍了为什么使用IOC容器,和IOC的设计思想以及IOC容器的优缺点,并且给大家转载了一篇介绍IOC原理的博文,我们这篇主要给大家依赖注入的两种方式,以及他们的优缺点. 我们这篇博文还是 ...
- SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...
- Spring、Spring依赖注入与编码剖析Spring依赖注入的原理
Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...
随机推荐
- [BZOJ]4650 优秀的拆分(Noi2016)
比较有意思的一道后缀数组题.(小C最近是和后缀数组淦上了?) 放在NOI的考场上.O(n^3)暴力80分,O(n^2)暴力95分…… 即使想把它作为一道签到题也不要这么随便啊摔(╯‵□′)╯︵┻━┻ ...
- Linux编程之内存池的设计与实现(C++98)
假设服务器的硬件资源"充裕",那么提高服务器性能的一个很直接的方法就是空间换时间,即"浪费"服务器的硬件资源,以换取其运行效率.提升服务器性能的一个重要方法就是 ...
- 在浏览器中运行Keras模型,并支持GPU
Keras.js 推荐一下网页上的 demo https://transcranial.github.io/keras-js/#/ 加载的比较慢,但是识别的非常快. Run Keras models ...
- jquery easyui datagrid改变某行的值
$("#DeterminateMembers").datagrid("updateRow",{index:index,row:{fzr:"0" ...
- Node.js HTTP
稳定性: 3 - 稳定 使用 HTTP 服务器或客户端功能必须调用 require('http'). Node 里的 HTTP 接口支持协议里原本比较难用的特性.特别是很大的或块编码的消息.这些接口不 ...
- PHP 字符串变量
PHP 字符串变量 字符串变量用于存储并处理文本. PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量 ...
- PHP 完整表单实例
PHP - 在表单中确保输入值 在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website. 在评论 ...
- 实验与作业(Python)-02 Python函数入门与温度转换程序
截止日期 实验目标 学会定义函数,使用函数.学会导入在某个文件中定义的函数. input获得值,然后通过eval或者int.float将其转换为相应的类型. 学会使用列表:访问列表.append.遍历 ...
- linux下数据同步、回写机制分析
一.前言在linux2.6.32之前,linux下数据同步是基于pdflush线程机制来实现的,在linux2.6.32以上的版本,内核彻底删掉了pdflush机制,改为了基于per-bdi线程来实现 ...
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...