于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>

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依赖注入的三种方式的更多相关文章

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

           在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...

  2. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

  3. Spring基础05——Spring依赖注入的三种方式

    Spring支持3种依赖注入的方式:属性注入.构造器注入.工厂 1.属性注入 属性注入即通过setter方法注入Bean的属性或依赖的对象.使用<property>元素,使用name属性指 ...

  4. spring——依赖注入的三种方式

    1 构造器注入(与构造器有直接关系) 默认无参构造 3种构造方式:通过<contructor-arg>调用类中的构造器 下标 <bean id="userService&q ...

  5. Spring静态注入的三种方式

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...

  6. SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式

    这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...

  7. Spring注解依赖注入的三种方式的优缺点以及优先选择

    当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...

  8. spring 依赖注入的3种方式

    在实际环境中实现IoC容器的方式主要分为两大类,一类是依赖查找,依赖查找是通过资源定位,把对应的资源查找回来:另一类则是依赖注入,而Spring主要使用的是依赖注入.一般而言,依赖注入可以分为3种方式 ...

  9. spring自动注入的三种方式

    所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...

随机推荐

  1. SOHO路由器的静态路由的不同

    网络拓扑如下,其中RA与RB皆为TP-LINK家用路由器 最终在TP-LINK官网的官网上找到这么一段话 静态路由是在路由器中手工设置的固定的路由条目.我司路由器静态路由是基于ICMP重定向原理,与其 ...

  2. android平台TextView使用ImageSpan画廊GIF图像

    android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...

  3. hdu1011(树形背包)

    hdu1011 http://acm.hdu.edu.cn/showproblem.php?pid=1011 给定n个洞穴和m个士兵(每个士兵能消灭20个bugs) 然后给定每个洞穴的bugs数量(背 ...

  4. 谈谈Linux内存释放

    上上周吧,一个朋友问我说他公司的服务器内存free 为0 是为什么,意思大概是内存去哪了,这引发了一个小小的讨论,也就是内存释放的问题… 首先我们可能会用free 去查看内存的使用率,它应该是这样的 ...

  5. SQLite Code配置DbConfiguration

    [DbConfigurationType(typeof(SQLiteConfiguration))] public partial class rsapiEntities : DbContext { ...

  6. Sublime Text 3 最性感的编辑历史

    ↑ ↑ ↑ ↑ ↑ 请参阅文件夹 ↑ ↑ ↑ ↑ ↑ 下载 / 装 windows / MAC OS 官网下载.双击安装,这个都会吧- linux linux下安装.一种办法是从官网下载 tar.bz ...

  7. 【SICP读书笔记(五)】练习2.32 --- 递归求集合子集

    题目内容: 我们可以将一个集合表示为一个元素互不相同的表,因此就可以将一个集合的所有子集表示为表的表.例如,假定集合为(1,2,3),它的所有子集的集合就是( () (3) (2) (2 3) (1) ...

  8. Java Web整合开发(78) -- Struts 1

    在Struts1.3中已经取消了<data-sources>标签,也就是说只能在1.2版中配置,因为Apache不推荐在 struts-config.xml中配置数据源.所以建议不要在st ...

  9. jsp简单练习-简单的下拉表单

    <%@ page contentType="text/html; charset=gb2312" %> <html> <body> <fo ...

  10. PHP读取Excel里的文件

    下载phpExcelReader  http://sourceforge.net/projects/phpexcelreader 解压后得到以下这些文件 jxlrwtest.xls这个excel文件有 ...