什么是依赖注入

先说什么是依赖

如下:

 class A{
B b;
}
class B{
}

则称A依赖B。

依赖:A的某些业务逻辑需要B的参与,如果不对A中的参数b进行实例化,那么A中的某些业务逻辑可能无法完成。则称A依赖B

依赖注入指的就是当A依赖B的时候,对A中的参数b进行实例化这个操作不再由程序猿来完成,而是通过Spring和配置文件来完成。

如下:

 package com.lille.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lille.item1.Item1;
import com.lille.item1.impl.Item1Impl;
import com.lille.service.SpringService;
import com.lille.service.impl.SpringServiceImpl; public class Test01 {
@Test
public void Demo() {
String xml="applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xml);
SpringService springService=(SpringService) applicationContext.getBean("springService");
springService.UserDesign();
// applicationContext.close();
}
@Test
public void Demo2(){
SpringService springService=new SpringServiceImpl();
Item1 item1=new Item1Impl();
((SpringServiceImpl)springService).setItem1(item1);
springService.UserDesign();
}
}

在上面那个方法中完全没有形如A.setB()样子的操作,而在下面那个方法中有

这是由于上面那个方法使用了依赖注入,A.setB()这个过程由Spring框架来完成。

事实上它们最终的输出都是一样的:

 package com.lille.service.impl;

 import com.lille.item1.Item1;
import com.lille.service.SpringService; public class SpringServiceImpl implements SpringService { private Item1 item1;
@Override
public void UserDesign() {
// TODO 自动生成的方法存根
System.out.println("hello world!");
try{
item1.print();
}catch(NullPointerException e){
System.out.println(e.getMessage());
}
}
public Item1 getItem1() {
return item1;
}
public void setItem1(Item1 item1) {
System.out.println("调用了一次setItem1方法");
this.item1 = item1;
} }
//输出结果
调用了一次setItem1方法
hello world!
com.lille.item1.impl.Item1Impl

这充分说明了即便是在依赖注入中,也是通过调用setItem1方法来将参数item1注入到SpringServiceImpl中的。

来看一下配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here -->
<bean id="springService" class="com.lille.service.impl.SpringServiceImpl">
<property name="item1" ref="item1"></property>
</bean>
<bean id="item1" class="com.lille.item1.impl.Item1Impl"></bean>
</beans>

看得出来这个配置文件中多了第九行和第十一行这两样东西。

第11行的意义很明确,不值得讨论。第九行的意义是:

当springService这个bean实例化之后,调用其setItem1()这个方法,将item1这个bean放进去。

这样做有个显著的好处,在使用依赖注入的情况下,源代码中没有出现SpringServiceImpl这个字段,事实上两次源代码甚至没有改动,而不使用依赖注入的情况下必须要出现SpringServiceImpl:第25行的((SpringServiceImpl)springService).setItem1(item1);

因为接口SpringService中没有setItem1()这个方法,要调用它就必须对springService进行向下转型。

按照接口写在实例化类之前的原则。

事实上,我们写这个SpringService接口的时候可能根本不不关心其实例化类的业务逻辑,更不知道最终的实例化类需要一个名为Item1的依赖对象,更不可能提前声明一个名为setItem1()的方法。

最新文章

  1. Power of Two
  2. C#窗体 WinForm 进程,线程
  3. (转)ConcurrentHashMap解析
  4. 使用BTRACE定位系统中慢的问题
  5. OpenJudge计算概论-矩阵归零消减序列和
  6. [转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
  7. erp中三大订单CO、PO、MO各是代表什么?
  8. For each db / table
  9. 伪元素::selection(怎么修改网页中被选中文本的样式)
  10. 11 款最好 CSS 框架 让你的网站独领风骚
  11. Ubuntu 12.04(所有ubuntu发行版都适用)sudo免输入密码
  12. appium初学者,使用之检查appium环境报错Could not detect Mac OS X Version from sw_vers output: &#39;10.12.1’,
  13. 利用reverse索引优化like语句的方法详解
  14. tomcat启动批处理——catalina.bat
  15. 2017-2018-2 20155228 《网络对抗技术》 实验三:MAL_免杀原理与实践
  16. cobbler批量化安装系统
  17. type的解释
  18. CentOS No package nginx available.
  19. Spark基本术语表+基本架构+基本提交运行模式
  20. 有关Delphi RTTI的一些文章

热门文章

  1. ROS Learning-025 (提高篇-003 A Mobile Base-01) 控制移动平台
  2. C语言-郝斌笔记-002病毒程序示范
  3. loj10131 暗的连锁
  4. Entity Framework Tutorial Basics(14):Choose development approach
  5. mysql--约束条件
  6. Algorithms - Merging Sort
  7. C#数据类型及差异(复习专用)
  8. 实现基于dotnetcore的扫一扫登录功能
  9. Bat 多个执行操作选择
  10. Hyperledger Fabric java chaincode 编译部署(1.4V)