什么是依赖注入

先说什么是依赖

如下:

 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. Linux kgdb命令

    一.简介 kgdb是一种源码级的Linux内核调试器.使用kgdb调试内核时,需要结合gdb一起使用,使用他们可以对内核进行单步调试,设置断点,观察变量.寄存器的值等与应用调试相关的功能.然而也有其限 ...

  2. Android studio中ShredPreferences 的简单使用

    ShredPreferences是一个轻量级的数据存储方式,只能存取字符串了整型数据这一类的数据,如果要存储复杂的数据这个存储方式就不再适用 首先是要新建一个ShredPreferences的对象 p ...

  3. Umbraco Form 中需要为一个Form的某个field设置特别的CSS样式

    在项目开发中,我有一个需求,需要建立一个页面,这个页面上有一个form, 这个form上有一个checkbox, 就是普通的接受terms & conditions, 类似下图 这个项目中的U ...

  4. Java 子类初始化过程

    //父类 class Animal{ private String name; private int age; Animal(String name, int age) {//创建父类构造器 sup ...

  5. Java泛型读书笔记 (三)

    泛型对于老代码的支持 Java的泛型设计成类型擦除的目的,很大一部分是为了兼容老老代码.如下的一段代码: void setLabelTable(Dictionary table) table的类型是非 ...

  6. VMWare虚拟机无法打开内核设备"\\.\Global\vmx86"的解决方法

    cmd执行: 1.net start vmci 2.net start vmx86 3.net start VMnetuserif

  7. signalR之java client的websocket BUG处理

    最近在用SignalR,服务端已经写好(老铁,没毛病,很稳),然后有坑的是我还得写App端,那就撸吧,java也不是什么很难的东西.奈何坑多(已经踩了一波android的控件bug),这次遇到了MS的 ...

  8. winform改变控件的外形

    GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 40, 40); Region region = new Region(gp); c ...

  9. Redis集群Windows

    一 所需软件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 安装配置redis redis下载地址   ...

  10. findControl 可以获取前台页面的控件

    findControl 可以获取前台页面的控件