什么是依赖注入

先说什么是依赖

如下:

 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()的方法。

傻瓜式Spring教学第二课的更多相关文章

  1. 傻瓜式Spring教学第一课

    首先,把Spring需要的五个包导入项目: commons-logging-1.2.jar spring-beans-4.3.4.RELEASE.jar spring-context-4.3.4.RE ...

  2. Spring入门第二课

    看代码 package logan.spring.study; public class HelloWorld { private String name; public void setName2( ...

  3. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  4. ES6新特性之傻瓜式说明

    ES6出来挺长一段时间了,但目前网上好像教程并不多也不详细.我依然遵循傻瓜式教学模式,白话文说明JavaScript和ES6的一些区别,说明下ES6的一些新特性.本文适合新手学习,大神请勿见笑,在下在 ...

  5. 傻瓜式理解递归之php递归

    写程序这么久了,有时候别人会问道一些算法比如排序啊,递归啊,总是不知道该怎么去说,今天就来整理一下,让更多的人去傻瓜式的理解递归.递归在网络上有很多定义,但有这么一句话听的最多:递归就是自己调用自己! ...

  6. 【C语言探索之旅】 第二部分第二课:进击的指针,C语言的王牌!

    内容简介 1.课程大纲 2.第二部分第二课: 进击的指针,C语言的王牌 3.第二部分第三课预告: 数组 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言 ...

  7. .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐

    作者:依乐祝 原本链接:https://www.cnblogs.com/yilezhu/p/9947905.html 引子 为什么写这篇文章呢?因为.NET Core的生态越来越好了!之前玩转.net ...

  8. 初识springboot(傻瓜式教程)

    初识springboot(傻瓜式教程) 项目所需的版本 IDEA 2018 maven 3.x jdk-1.8 IDEA创建spring-boot项目(maven方法) 1.创建一个maven工程 点 ...

  9. 【原创 深度学习与TensorFlow 动手实践系列 - 2】第二课:传统神经网络

    第二课 传统神经网络 <深度学习>整体结构: 线性回归 -> 神经网络 -> 卷积神经网络(CNN)-> 循环神经网络(RNN)- LSTM 目标分类(人脸识别,物品识别 ...

随机推荐

  1. Codeforces 1077E (二分乱搞或者dp)

    题意:给你一个数组,可以从中选区若干种元素,但每种元素选区的个数前一种必须是后一种的2倍,选区的任意2种元素不能相同,问可以选取最多的元素个数是多少? 思路1(乱搞):记录一下每种元素的个数,然后暴力 ...

  2. Struts2框架06 ValueStack

    原文地址:点击前往 1 什么是ValueStack 称为值栈,Struts提供的共享数据的数据结构 2 为什么要使用ValueStack 从控制器向浏览器传递数据 存储与请求相关的对象信息(sessi ...

  3. MSScriptControl详解(可实现在C#等语言中调用JAVASCRIPT代码)

    ScriptControl接口 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object ...

  4. 高性能MySQL笔记-第5章Indexing for High Performance-002Hash indexes

    一. 1.什么是hash index A hash index is built on a hash table and is useful only for exact lookups that u ...

  5. Luogu 4363 [九省联考2018]一双木棋chess

    发现数据范围很小,想到状压dp,然后就愣住不会了. 表示太菜了并没有接触过轮廓线dp这种操作. 首先发现合法的操作过程中一定是这样子的: 按照行来看发现每一行单调不递增. 我们用$1$来表示竖着的轮廓 ...

  6. Json Post到 https的坑 - the underlying connection was closed an unexpected error occurred on a send(远程服务器未知错误导致关闭)

    最近做了一个安装包,安装包会弹出dotnet的 窗体,这个安装包会去调用https的一个api.用测试程序测试窗体都是好的.一旦打入安装包后,就报错.研究了半天,原来是https惹的祸 解决方案: . ...

  7. C++面试笔记--面向对象

    说到面向对象,大家第一反应应该就是它的三大特性:封装性.继承性和多态性.那么我们先简单的了解一下这三大特性: (1)封装性:封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的 ...

  8. [译]如何在visual studio中调试Javascript

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  9. web.xml文件的Url-pattern 节点配置

  10. PostgreSQL 设置主键的序列值

    1. 问题的提出 PostgreSQL定义TABLE时,主键的字段类型可以设定为自增类型serial,即插入每条记录时,主键的值自动加1.但是,当插入数据的时候指定了具体的主键值,例如主键值从0到50 ...