Spring IOC&DI

控制反转(inversion of control):控制什么?什么反转?

  我们都知道,传统的程序中,如果A类需要使用B类对象,会在程序中直接创建B类对象实例,此时创建对象的控制权在A类手上;但现在出现了一个IOC/DI容器,由IOC/DI容器代替A类执行实例化B类的权利,所谓控制权由A类转向IOC容器,即控制反转。

依赖注入(Dependency Injection):什么是依赖,谁依赖谁,什么是注入,谁注入,注入哪里?

  依赖就是依靠依附(鱼依赖水),对象依赖IOC/DI容器,注入:IOC/DI容器将某个对象的资源注入到所需要该对象资源的对象中;从IOC/DI容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,相当于为调用者注入他所依赖的实例。

总结:控制反转是原则,依赖注入是实现,控制反转的从程序角度出发描述概念,依赖注入从Spring容器角度出发描述概念。

Spring IOC实现

  BeanFactory:提供完整的Ioc服务支持,是一个管理Bean的工厂,负责初始化各种bean,有多个实现类,例如:XmlBeanFactory(该类根据XML配置文件中的定义来装配Bean)

  ApplicationContext:是BeanFactory的子接口,除了支持BeanFactory的功能外,还支持国际化,资源访问等内容

创建ApplicationContext接口实例的方法有三种

  1. ClassPathXmlApplicationContext(相对路径)
  2. FileSystemXmlApplicationContext(绝对路径)
  3. 通过Web服务器实例化ApplicationContext(web.xml文件中) 此种方式自己还未理解什么意思

依赖注入类型

使用构造方法注入

  Dao&DaoImpl

public interface TestDIDao {
void sayhello();
} import com.dao.TestDIDao;
public class TestDIDaoImpl implements TestDIDao{
@Override
public void sayhello() {
System.out.println("Hello World!");
}
}

  Service&ServiceImpl

public interface TestDIService {
public void sayHello();
} public class TestDIServiceImpl implements TestDIService {
public TestDIDao testDIDao;
//构造方法,用于实现依赖注入接口对象testDIDao
public TestDIServiceImpl(TestDIDao testDIDao) {
super();
this.testDIDao = testDIDao;
}
@Override
public void sayHello() {
//调用testDIDao中的sayHello方法
testDIDao.sayhello();
System.out.println("TestDIService依赖注入调用TestDIDao中的方法....");
}
}

  Xml

<!-- 将指定的类TestDaoImpl配置给Spring,让Spring创建该实例 -->
<bean id="testDIDao" class="com.dao.impl.TestDIDaoImpl" />
<!-- 使用构造方法注入 -->
<bean id="testDIService" class="com.service.impl.TestDIServiceImpl">
<!--将myTestDADao注入到TestDIServiceImpl类的属性testDIDao上-->
<constructor-arg index="0" ref="testDIDao"></constructor-arg>
</bean>

  Test

//测试用构造器方式注入
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取testDIService实例,测试构造方法注入
TestDIService testDIService = (TestDIService) context.getBean("testDIService");
testDIService.sayHello();

使用属性注入

  Dao&DaoImpl

public interface TestDIDao {
void sayhello();
} import com.dao.TestDIDao;
public class TestDIDaoImpl implements TestDIDao{
@Override
public void sayhello() {
System.out.println("Hello World!");
}
}

  Service&ServiceImpl

public interface TestDIService {
public void sayHello();
} public class TestDIServiceImpl implements TestDIService {
  public TestDIDao testDIDao;
//使用setter方法实现注入
public void setTestDIDao(TestDIDao testDIDao) {
this.testDIDao = testDIDao;
}
@Override
public void sayHello() {
//调用testDIDao中的sayHello方法
testDIDao.sayhello();
System.out.println("TestDIService依赖注入调用TestDIDao中的方法....");
}
}

XML

<!-- 将指定的类TestDaoImpl配置给Spring,让Spring创建该实例 -->
<bean id="testDIDao" class="com.dao.impl.TestDIDaoImpl" />
<!-- 使用属性方法注入 -->
<bean id="testDIService" class="com.service.impl.TestDIServiceImpl">
  <!-- 调用TestDIServiceImpl类的setter方法,将myTestDADao注入到TestDIServiceImpl类的属性testDIDao上 -->
  <property name="testDIDao" ref="testDIDao"></property>
</bean>

Test

//测试用属性方式注入
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDIService testDIService = (TestDIService)context.getBean("testDIService");
testDIService.sayHello();

参考博文:

  https://blog.csdn.net/doris_crazy/article/details/18353197

https://www.cnblogs.com/zanpen2000/p/7810884.html

https://www.cnblogs.com/leoo2sk/archive/2009/06/17/di-and-ioc.html

同样在自学Spring的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!

Spring入门(二)的更多相关文章

  1. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  2. Spring入门二:整合mybatis

    一.SM思路分析 1.引入核心依赖及相关依赖:  spring(略).mybatis.mysql.mybatis-spring(减少自己实现FactoryBean接口).druid <depen ...

  3. spring入门(二) 使用注解代替xml配置

    1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  4. Spring入门详细教程(二)

    前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...

  5. Spring Boot入门(二):获取配置文件值

    本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...

  6. Spring(二)--Spring入门案例

    Spring入门案例 1.需要的实体类 2.需要的接口和实现类 3.需要的service和实现类 /** * service层的作用 * 在不改变dao层代码的前提下,增加业务逻辑操作 */ publ ...

  7. spring 入门篇

    spring 入门篇         相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...

  8. spring入门详细教程(五)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  9. Spring入门详细教程(四)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  10. Spring入门详细教程(三)

    前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...

随机推荐

  1. 数据结构与算法简记--redis有序集合实现-跳跃表

    跳表 定义 为一个值有序的链表建立多级索引,比如每2个节点提取一个节点到上一级,我们把抽出来的那一级叫做索引或索引层.如下图所示,其中down表示down指针,指向下一级节点.以此类推,对于节点数为n ...

  2. [USACO14MAR]浇地Watering the Fields

    题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...

  3. 出现异常: 非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复: required

    在将web.config文件中的<add key="ClientValidationEnabled" value="false" /> 设为fals ...

  4. idae父子项目Test执行报Result Maps collection already contains value for xxx

    现象:同一个springmvc工程使用eclipse和idea用Tomcat启动都没问题,但是如果走单元测试使用到了@ContextConfiguration这个spring的上下文注解idea出问题 ...

  5. Android开发环境部署:JDK+Android Studio

    1. 刚开始接触Android开发,首先需要为你的电脑安装java JDK(Java开发工具包),不管是用Eclipse还是Android Studio都需要只吃Java语言运行吧. 官网:Oracl ...

  6. word2vec 原理浅析 及高效训练方法

    1. https://www.cnblogs.com/cymx66688/p/11185824.html (word2vec中的CBOW 和skip-gram 模型 浅析) 2. https://ww ...

  7. Centos7上MariaDB数据库启动问题解决

    安装MariaDB数据库后出现服务启动失败问题, 解决办法:卸载再安装!(确定无3306端口占用) 一.卸载数据库: [root@localhost logs]# yum -y remove mari ...

  8. 62. File类常用方法

    为了怕混淆,先说明一些下面要出现的名词意思:例如:D:\\新建文件夹 (2)\\a.txt 和  D:\\新建文件夹 (2)\\aaaa D:\\新建文件夹 (2)   父路径    a.txt    ...

  9. Vue学习笔记【25】——Vue组件(组件间传值)

    父组件向子组件传值 组件实例定义方式,注意:一定要使用props属性来定义父组件传递过来的数据  <script>    // 创建 Vue 实例,得到 ViewModel    var ...

  10. 揭秘阿里云EB级大数据计算引擎MaxCompute

    日前,全球权威咨询与服务机构Forrester发布了<The Forrester WaveTM: Cloud Data Warehouse, Q4 2018>报告.这是Forrester ...