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>
client測试类
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>
效果图
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzaGVoZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
总结
对于Spring的依赖注入。最重要的就是理解他的,一旦理解了,将会认为很的简单。无非就是让容器来给我们实例化那些类。我们要做的就是给容器提供这个接口。这个接口就我们的set方法或由构造。
版权声明:本文博主原创文章,博客,未经同意不得转载。
SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式的更多相关文章
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
- Spring基础05——Spring依赖注入的三种方式
Spring支持3种依赖注入的方式:属性注入.构造器注入.工厂 1.属性注入 属性注入即通过setter方法注入Bean的属性或依赖的对象.使用<property>元素,使用name属性指 ...
- spring——依赖注入的三种方式
1 构造器注入(与构造器有直接关系) 默认无参构造 3种构造方式:通过<contructor-arg>调用类中的构造器 下标 <bean id="userService&q ...
- Spring静态注入的三种方式
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...
- SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...
- Spring注解依赖注入的三种方式的优缺点以及优先选择
当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...
- spring 依赖注入的3种方式
在实际环境中实现IoC容器的方式主要分为两大类,一类是依赖查找,依赖查找是通过资源定位,把对应的资源查找回来:另一类则是依赖注入,而Spring主要使用的是依赖注入.一般而言,依赖注入可以分为3种方式 ...
- spring自动注入的三种方式
所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...
随机推荐
- socket用法
INADDR_ANY是ANY,是绑定地址0.0.0.0上的监听, 能收到任意一块网卡的连接:INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127 ...
- HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram
HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram ...
- Conversion to Dalvik format failed with error 1
主要和添�的第三方的包有关系. ======================================= 出现,Conversion to Dalvik format failed with e ...
- Xamarin For Visual Studio 3.7.165 完整离线破解版
原文 Xamarin For Visual Studio 3.7.165 完整离线破解版 Xamarin For Visual Studio就是原本的Xamarin For Android 以及 Xa ...
- HiPAC高性能规则匹配算法之查找过程
收到一封邮件,有位朋友认为我误解了nf-HiPAC.如此的一个高性能算法怎能被什么传统的hash,tree之类的胁迫.是啊.HiPAC是一个非常猛的算法.文档也比較少,这就更加添加了其神奇感,可是这决 ...
- hdu - 4975 - A simple Gaussian elimination problem.(最大流量)
意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...
- 【剑指offer】面试题26:复制的复杂链条
def copyRandomList(self, head): if None == head: return None phead = head while phead: pnext = phead ...
- java线程池的注意事项
java线程池是建立一个技术进步的线程.到来之前线程池打造一批在线程的线程尚未,队列上的备用,然后再利用这些资源.减少频繁创建和销毁对象, 1.jdk1.5以上提供了现成的线程池 2.java线程池的 ...
- uva 1393 - Highways(容斥原理)
题目连接:uva 1393 - Highways 题目大意:给定一个m∗n的矩阵,将矩阵上的点两两相连,问有多少条直线至少经过两点. 解题思路:头一次做这样的题目,卡了一晚上. dp[i][j]即为i ...
- Vc6.0打开该文件坠毁
这是一个微软bug,下面是用户给出的溶液: http://blog.163.com/wjatnx@yeah/blog/static/12758622820138110530322/ http://bl ...