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提 ...
随机推荐
- mysql替换字段里数据内容部分字符串(亦可用于增加字段中的内容)
mysql替换表的字段里面内容,如例子: mysql> select host,user from user where user='testuser'; +----------------- ...
- Android开发中验证码的生成
近期在做电商金融类的项目,验证码的生成方法不可缺少.先学习了一种.经过測试好用.从别处学习的代码,稍修改了一下可选择是否支持识别大写和小写.直接上代码. import android.app.Acti ...
- SQL Server :理解IAM 页
原文:SQL Server :理解IAM 页 在以前的文章里,我们讨论了数据页,GAM和SGAM,还有PFS页.今天我们一起来讨论下索引分配映射(Index Allocation Map:IAM)页. ...
- 蚁群算法和简要matlab来源
1 蚁群算法原理 从1991由意大利学者 M. Dorigo,V. Maniezzo 和 A. Colorni 通过模拟蚁群觅食行为提出了一种基于群体的模拟进化算法--蚁群优化.极大关注,蚁群算法的特 ...
- linux 流量监控 ---iptraf的安装及使用
一.安装iptraf 我用的是centos,切换到root用户,执行 yum install -y iptraf 二.使用 1.直接输入iptraf,进入软件,按任意键继续 2.我主要是第二项和第三项 ...
- 基础知识(1)- Java程序设计概述
1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1 简单性 1.2.2 面向对象 1.2.3 网络技能 1.2.4 健壮性 1.2.5 安全性 1.2.6 体系 ...
- 将jar要么aar公布到到网mvn 在(使用github作为仓库), 通过gradle dependency 信息集成
使用Android Studio用户开发,都希望通过maven该方式整合远程仓库jar.aar文件.但如何将这些文件发布它? 发人员都会将jar文件公布到sonatype上,以提供给其它开发人员集成, ...
- TimesTen更改CacheGroup管理用户ORACLE结束和TT结束password【TimesTen操作和维修基地】
password管理一直操作的一部分的安全管理和维护.CacheGroup管理用户password虽然并不复杂变化.然而,这是用于生产,改不好比较easy导致失败.简介点击这里CacheGroup管理 ...
- .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间
下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...
- [LeetCode202]Happy Number判断一个数是不是happy number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...