1. IOC的概念

控制反转IoC(Inversion of Control)是一种设计思想,而DI(依赖注入)是实现IoC的一种方法。在没有使用IOC的程序中,对象间的依赖关系是靠硬编码的方式实现的。引入IOC后对象的创建由程序自己控制的,控制反转即将对象的创建交给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。

IoC是Spring框架的核心内容,在IOC容器中一切对象皆为Bean组件。IOC容器通过读取XML配置文件中的Bean信息,产生每个Bean实例。也可以使用注解,新版本的Spring也可以零配置实现IoC。

2.  使用XML配置文件实现依赖注入

/**人*/
public abstract class Person {public String name;
}

/**学生*/
public class Student extends Person {/**身高*/public int height;/**有参构造方法*/public Student(String name,int height){this.name=name;this.height=height;}@Overridepublic String toString() {return "Student{" + "height=" + height+",name="+name +'}';}
}

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class School {public static void main(String[] args) {//IoC容器ApplicationContext ctx=new ClassPathXmlApplicationContext("beans02.xml");//从容器中获取对象Person tom=ctx.getBean("tom",Person.class);System.out.println(tom);}
}

2.1 通过参数名来注入

<?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 id="tom" class="spring02.Student"><constructor-arg name="name" value="张柏川"></constructor-arg><constructor-arg name="height" value="195"></constructor-arg></bean>
</beans>

2.2 通过参数下标来注入

<bean id="rose" class="spring02.Student"><constructor-arg index="0" value="张柏芝"></constructor-arg><constructor-arg index="1" value="196"></constructor-arg>
</bean>

2.3 通过属性赋值

<bean id="jack" class="spring02.Student"><property name="name" value="张三"></property ><property  name="height" value="195"></property ></bean>

注意:这种通过property 注入的方式,只能对有构造器(即有set和get方法)的属性注入,对与没有构造器的属性不能用这种方式注入

特殊情况:若注入的属性为引用类型,则通过ref指定属性值

Student.java

/**学生*/
public class Student {/**姓名*/String name;/**身高*/public int height;/**地址*/public Address address;/**有参构造方法*/public Student(String name,int height){this.name=name;this.height=height;}/**有参构造方法*/public Student(String name,int height,Address address){this.name=name;this.height=height;this.address=address;}@Overridepublic String toString() {return "Student{" + "height=" + height+",name="+name +'}'+address;}
}

Address.java

/**地址*/
public class Address {/**国家*/private String country;/**城市*/private String city;public Address() {}public Address(String country, String city) {this.country = country;this.city = city;}@Overridepublic String toString() {return "Address{" +"country='" + country + '\'' +", city='" + city + '\'' +'}';}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}
}

通过配置文件注入引用类型属性

<?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 name="zhuhai" class="spring02.Address"><property name="country" value="中国"></property><property name="city" value="珠海"></property></bean><bean id="tom" class="spring02.Student"><constructor-arg name="name" value="张柏川"></constructor-arg><constructor-arg name="height" value="195"></constructor-arg><constructor-arg name="address" ref="zhuhai"></constructor-arg></bean><bean id="rose" class="spring02.Student"><constructor-arg index="0" value="张柏芝"></constructor-arg><constructor-arg index="1" value="196"></constructor-arg><constructor-arg index="2" ref="zhuhai"></constructor-arg></bean>
</beans>

测试代码:

package spring02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class School {public static void main(String[] args) {//IoC容器ApplicationContext ctx=new ClassPathXmlApplicationContext("bookbean01.xml","beans02.xml");//从容器中获取对象Person tom=ctx.getBean("tom",Person.class);Person rose=ctx.getBean("rose",Person.class);//Address zhuhai=ctx.getBean("zhuhai",Address.class);
        System.out.println(tom);System.out.println(rose);}
}

2.4 scope属性控制从容器中取回对象的作用域

从容器中取回的对象默认是单例的

Person roseA=ctx.getBean("rose",Person.class);Person roseB=ctx.getBean("rose",Person.class);//Address zhuhai=ctx.getBean("zhuhai",Address.class);
        System.out.println(tom);System.out.println(roseA==roseB);

运行结果:

scope属性可取值如下:

scope属性取值prototype的实例:

<bean id="rose" class="spring02.Student" scope="prototype"><constructor-arg index="0" value="张柏芝"></constructor-arg><constructor-arg index="1" value="196"></constructor-arg><constructor-arg index="2" ref="zhuhai"></constructor-arg></bean>

//从容器中获取对象Person tom=ctx.getBean("tom",Person.class);Person roseA=ctx.getBean("rose",Person.class);Person roseB=ctx.getBean("rose",Person.class);//Address zhuhai=ctx.getBean("zhuhai",Address.class);
        System.out.println(tom);System.out.println(roseA==roseB);

运行结果:

2.5 lazy-init属性控制Bean的延迟实例化

ApplicationContext默认是在启动时将所有 singleton bean提前实例化。 通常这样的提前实例化方式是好事,因为配置中或者运行环境的错误就会被立刻发现,否则可能要花几个小时甚至几天。如果你不想这样,你可以将单例bean定义为延迟加载防止它提前实例化。延迟初始化bean会告诉Ioc容器在第一次需要的时候才实例化而不是在容器启动时就实例化。

实例:

<bean id="lazytom" class="spring_IoC.dao.Student" lazy-init="true" scope="prototype">    <constructor-arg name="name" value="张柏川"></constructor-arg>    <constructor-arg name="height" value="195"></constructor-arg></bean>

    public static void main(String[] args) {//IoC容器ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");     try {Thread.sleep(2000);//从容器中获取对象Person tom1=ctx.getBean("lazytom",Person.class);System.out.println("tom1初始化了");Thread.sleep(2000);Person tom2=ctx.getBean("lazytom",Person.class);System.out.println("tom2初始化了");System.out.println(tom1==tom2);} catch (InterruptedException e) {e.printStackTrace();}}

结果:

tom1初始化了
tom2初始化了
false

转载于:https://www.cnblogs.com/leiblog/p/10654136.html

【Java_Spring】控制反转IOC(Inversion of Control)相关推荐

  1. IOC 控制反转(Inversion of Control,英文缩写为IoC)

    在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 在这样的齿轮组中,因为是协同工作,如果有一个齿轮出了问题,就可能会影响到整个齿 ...

  2. 什么是控制反转(Inversion of Control)

    [2020.11 最后编辑] 要点:IoC是对框架特征的一般性描述:每一种框架用途不一,永远不要问某个框架"哪些方面的控制被反转了呢?"这种愚蠢的问题.JUnit是Java语言著名 ...

  3. [Js-Spring]Spring与IoC(控制反转,Inversion of Control)

    控制反转(Ioc,Inversion of Control),是一个概念,一种思想.指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理.控制反转就是对对象控制权的转移,从 ...

  4. 依赖倒置(DIP),控制反转(IoC)与依赖注入(DI)

    DIP,IoC与DI概念解析 依赖倒置 DIP(Dependency Inversion Principle) DIP的两大原则: 1.高层模块不应该依赖于低层模块,二者都应该依赖于抽象. 2.抽象不 ...

  5. 浅析Spring——控制反转IoC

    目录 1. IoC理论推导 2. 什么是IoC? 3. 引入DI 4. IoC容器 5. 注入对象的四种方法 1.基于接口 2.基于setter 3.基于构造函数 4.基于注解 6. 两种IoC实现方 ...

  6. 代码的演化-DI(理解依赖注入di,控制反转ioc)

    控制反转(Inversion of Control IoC)在java中,Spring就是一个很好的应用.用于解除使用者和生产者的耦合. 一般的代码中.使用者即是生产者,使用者在调用它需要的对象的时候 ...

  7. 庖丁解牛Nop:控制反转--IOC和DI

    IOC中文名被称作控制反转(Inversion of Control),DI被称为依赖注入(Dependency Injection); 使用控制反转开发模式是先开发接口,然后再实现类.这种方法却可以 ...

  8. Spring 概念及特点 Spring下载地址 控制反转IoC实现原理

    Spring下载地址 http://repo.springsource.org/libs-release-local/org/springframework/spring/ Spring是开源full ...

  9. 前端解读控制反转(IOC)

    前言 随着前端承担的职责越来越重,前端应用向着复杂化.规模化的方向发展.大型项目模块化是一种趋势,不可避免模块之间要相互依赖,此外还有很多第三方包.这样的话如何去管理这些繁杂的文件,是一个不可避免的话 ...

  10. java-12:spring MVC - 控制反转IOC,依赖注入DI

    学习spring框架之前,先理解几个概念: 1.第一部分:依赖倒置原则 2.第二部分:控制反转,控制反转容器(实例) 3.第三部分:控制反转,控制反转容器(全面理解,面试题) 综合性理解:控制反转(I ...

最新文章

  1. tensorflow打印模型结构_Tensorflow上手1: Print与py_func
  2. @RequestBody注解失效?从前端传来的数据到底需不需要@RequestBody注解?前端传输数据解析的问题?
  3. sae上部署第一个站
  4. VS2008中配置 Windows SDK v7
  5. c++ map 析构函数_C++|类继承关系中的虚函数、虚析构函数、虚基类
  6. Java中带有JWebSocket的WebServerSocket
  7. java mvc中重复提交表单,spring mvc 防止重复提交表单的两种方法,推荐第二种
  8. Vijos1775 CodeVS1174 NOIP2009 靶形数独
  9. 创始人的领导力和合伙人选择
  10. php版本与vc运行库
  11. c语言中头文件iostream,程序中为什么要包含头文件iostream.h?
  12. 微信小程序实例练习——《排班查询》
  13. c语言输出最大的数ns流程图_急!!!!c语言NS流程图
  14. python常用内置库时间,日期与JSON转换
  15. Web漏洞扫描工具(批量脱壳、反序列化、CMS)
  16. java主机哪儿好_java虚拟主机哪个好,香港java虚拟主机哪里有!
  17. 【php】php开发环境的搭建
  18. mysql创建用户表的sql语句,mysql创建表的sql语句
  19. 基于web的商场商城后台管理系统
  20. 用python做视觉检测系统_教你用 Python 做一个物体检测系统

热门文章

  1. BGP路由反射器原理和实现(华为设备)
  2. docker的安装与加速器的配置
  3. linux下各种解压方法
  4. [ CSS ] animation 快速参考
  5. Apache无法加载PHP模块的解决方案
  6. 测试h265和h264的编码效果
  7. Java判断文件编码格式
  8. 引擎工具开发的一些总结
  9. 如何判断各个IE浏览器版本
  10. 使用脚本开启客户端远程桌面