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. webservice的使用-axis1-01

    1.搭建axis服务器 1.1 下载axis-bin-1_4.zip文件并解压 1.2 拷贝\axis-1_4\webapps目录下的axis到tomcat目录下的webapps目录下并启动 1.3 ...

  2. react使用总结

    1.拿到页面首先需要设计好,每个组件该怎么实现,划分好组件可以减少重复代码,有的时候需要和后端确认才能形成正确的划分 2.页面上的需要展示的数据都是由后端数据而来,所以任何增删改查的数据都要从后端重新 ...

  3. Vmware虚拟机中安装ubuntu18 live server+Vmware Tools(用来共享本地文件夹)

    一.安装Ubuntu见链接 https://ywnz.com/linuxaz/3696.html 二.安装Vmware Tools 参考:https://blog.csdn.net/a12340123 ...

  4. 总结下awk基本用法

    命令格式: awk '{commands} [{other commands}]' awk 'condition{commands} [{other commands}]' 如:awk '$4==&q ...

  5. C—变量

    C—变量 在C语言中,变量要先定义后使用. 使用时,必须说明变量的存储类型与数据类型. 变量说明的一般形式: <存储类型>  <数据类型>  <变量名> 存储类型的 ...

  6. mongoose 常用数据库操作 查询

    条件查询 Model.find(conditions, [fields], [options], [callback]) demo1 try.js var User = require(". ...

  7. php获取微信openid

    使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的 ...

  8. Java 基础 - System.arraycopy() 浅拷贝 深拷贝

    ref: https://blog.csdn.net/balsamspear/article/details/85069207 https://blog.csdn.net/balsamspear/ar ...

  9. jq随机生成数字加字母的字符串

    html代码: <dl class="row"> <dt class="tit"> <label for="title& ...

  10. python自动化测试-使用第三方python库技术实现

    转载自https://www.cnblogs.com/beer/p/5418471.html