首页 技术 正文
技术 2022年11月24日
0 收藏 502 点赞 2,905 浏览 4141 个字

       于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方法或由构造。

版权声明:本文博主原创文章,博客,未经同意不得转载。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844