什么是依赖注入

先说什么是依赖

如下:

 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 #528 Div2 F (1087F) Rock-Paper-Scissors Champion 树状数组+set

    题意:n个人站成一排,初始时刻每个人手中都有一个图案,可能是石头,剪刀,布3个中的1种,之后会随机选取相邻的两个人玩石头剪刀布的游戏,输的人会离开(如果两个人图案相同,则随机选择一个人离开).执行(n ...

  2. cd命令无效

    原因是没有切换盘符步骤一:C:\Documents and Settings\Administrator>d:步骤二:cd D:\Program Files\Python35-32\Script ...

  3. return()函数

    在函数中,执行完return()函数后,下面的语句就不会再执行了.例子: <?php function fn() { echo "you are awsome"; retur ...

  4. c++的单例模式及c++11对单例模式的优化

    单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能会优先考虑使用全局或者静态 ...

  5. Easyui datebox单击文本框显示日期选择 eayui版本1.5.4.1

    Easyui默认是点击文本框后面的图标显示日期,体验很不好,所以我想单击文本框就显示日期选择框,网上很多版本是1.3,1.4的,于是自己就比葫芦画瓢改了一个1.5.4.1的版本. 我参考了网上这个帖子 ...

  6. IB使用

    A:给控件添加方法或变量. 1.窗口上拖个控件 NSButton 2..点右上那张狗脸(Editor)对上的. 3.右键控件.拖到头文件中. 4 .选择加响应方法或变量.

  7. 关于Java中hashCode方法的实现源码

    首先来看一下String中hashCode方法的实现源码. public int hashCode() { int h = hash; if (h == 0 && value.leng ...

  8. SSH (Struts2+Spring3.0+Hibernate3)框架(一) 理论

    典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...

  9. Xshell连接linux(deepin)时提示ssh服务器拒绝了密码,请再试一次解决方法

    用Xshell root连接时显示ssh服务器拒绝了密码,应该是应该是sshd的设置不允许root用户用密码远程登录 修改 /etc/ssh/sshd_config文件,注意,安装了openssh才会 ...

  10. 机器学习基石笔记:11 Linear Models for Classification、LC vs LinReg vs LogReg、OVA、OVO

    原文地址:https://www.jianshu.com/p/6f86290e70f9 一.二元分类的线性模型 线性回归后的参数值常用于PLA/PA/Logistic Regression的参数初始化 ...