Spring——IoC
控制反转(Inversion ofControl,英文缩写为IoC)是一种能够解耦的方法,不是什么技术。是一种思想,也是轻量级的Spring框架的核心。控制反转一般分为两种类型。依赖注入(DependencyInjection。简称DI)和依赖查找。
控制反转是,关于一个对象怎样获取他所依赖的对象的引用。这个责任的反转。
我们通过一个样例体会IoC的优点:
数据模型例如以下:
Human接口:
package sping.tgb.ioc;
public interface Human {
public void eat();
public void sleep();
}
Student类:
package sping.tgb.ioc;
public class Student implements Human {
public void eat() {
System.out.println("学生吃饭");
}
public void sleep() {
System.out.println("学生睡觉");
}
}
Teacher类:
package sping.tgb.ioc;
public class Teacher implements Human {
public void eat() {
System.out.println("老师吃饭");
}
public void sleep() {
System.out.println("老师睡觉");
}
}
用工厂模式:
我们假设要在client调用这两个类,不用控制反转的话,能够用工厂实现,例如以下:
工厂类:
package sping.tgb.ioc;
public class Factory {
public final static String TEACHER = "teacher";
public final static String STUDENT = "student";
public Human getHuman(String ethnic) {
if (ethnic.equals(TEACHER))
return new Teacher();
else if (ethnic.equals(STUDENT))
return new Student();
else
throw new IllegalArgumentException("參数(职业)错误");
}
}
client:
public static void main(String[] args) {
Human human =null;
human=new Factory().getHuman(Factory.STUDENT);
human.eat();
human.sleep();
human=new Factory().getHuman(Factory.TEACHER);
human.eat();
human.sleep();
}
输出结果:
用Spring:
用工厂我们做到了解耦和。client不用知道详细的调用类,由工厂推断。以下我们看一下用控制反转我们怎样做:
使用spring。我们就不用写工厂类,直接写配置文件,例如以下:
client:
public static void main(String[] args) {
BeanFactory beanFactory=null;
beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
Human human = null;
human = (Human) beanFactory.getBean("student");
human.eat();
human.sleep();
human = (Human) beanFactory.getBean("teacher");
human.eat();
human.sleep();
}
输出结果:
对照:
大家能够看到用IoC和用工厂的作用基本是一致的,那为什么还要用IoC呢?优势在哪里?
Spring做到了解耦,上一层不依赖下一层的详细类,相同。工厂也做到了。
可是用Spring当需求发生变化时仅仅要改动配置文件,不用又一次编译系统;而用工厂当需求发生变化须要添加或删除、改动类时,须要又一次编译。这样能够说Spring做到了热插拔。
总结:
IoC的灵活性是有代价的:设置步骤麻烦、生成对象的方式不直观、反射比正常生成对象在效率上慢一点。因此并非不论什么地方都适合用IoC的。用到工厂模式的地方都能够考虑用IoC模式。
Spring——IoC的更多相关文章
- 【初探Spring】------Spring IOC(三):初始化过程---Resource定位
我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...
- 【初探Spring】------Spring IOC(一)
IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...
- spring ioc
spring ioc是spring的核心之一,也是spring体系的基础,那么spring ioc所依赖的底层技术是什么的?反射,以前我们开发程序的时候对象之间的相互调用需要用new来实现,现在所有的 ...
- Spring IoC源码解析——Bean的创建和初始化
Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...
- spring笔记6 spring IOC的中级知识
1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...
- 谈谈对Spring IOC的理解(转)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- 自己动手编写spring IOC源码
前言:对于spring IOC概念不是很了解的朋友可以阅读我上一篇博客--轻松理解spring IOC(这两篇博客也是由于我的个人原因导致现在才发布,惭愧啊).通过这篇博客的理解之后,相信大家会对sp ...
- spring ioc 源码解析
什么是ioc? 通俗的解释是:(spring)框架中,完成对象的创建和注入的容器. springIOC体系结构: spring IOC的创建是典型的工厂模式,这一系列的bean工厂如上所示. 其核心是 ...
- Spring:源码解读Spring IOC原理
Spring IOC设计原理解析:本文乃学习整理参考而来 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. I ...
- 谈谈对Spring IOC的理解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
随机推荐
- vue工厂化完整项目目录
- mac 下安装python pil
mac:sudo brew install freetype sudo pip install pillow ubuntu: sudo apt-get install libfreetype6-dev ...
- 阿里云 Django部署参考
Linux下安装Python3和django并配置mysql作为django默认服务器 CentOS7.3安装Python3.6 yum except KeyboardInterrupt, e: 错误 ...
- js判断图片是否有效
var ImgObj=new Image(); ImgObj.src= 'http://192.168.10.6:8082/3D/SERVER_1_DELL_880.jpg'; if(ImgObj.f ...
- 查看用户的信息文件-passwd
passwd 文件 位置:/etc/passwd 作用:用于保存用户的账户信息 注意点:由于passwd也可以作为一个命令直接使用,也可以作为配置文件,所以如果使用man命令进行查看帮助信息时,应该有 ...
- 零基础入门学习Python(16)--序列!序列!
前言 你可能发现了,小甲鱼把这个列表,元组,字符串放在一起讲是有道理的,它们有许多共同点: 都可以通过索引得到每一个元素 默认索引值总是从0开始 可以通过分片的方法得到一个范围内的元素的集合 有很多共 ...
- composer install提示需要输入账号解决方法
1.问题描述:输入composer install提示需要输入账号,如下所示: 2.解决方法,改用社区的源:composer config -g repo.packagist composer htt ...
- LVS-NAT负载均衡PHP应用(Wordpress、Discuz)
1 实验拓扑 2 需求 RS-01和RS-02对外提供WEB服务. RS-01搭建LAMP,PHP通过http模块方式提供. RS-02搭建LAMP,PHP通过fpm方式提供. RS-01和RS-02 ...
- matplotlib.pyplot.pcolormesh
matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, shading ...
- C语言学习6
int i; 定义整形变量i int *p; p为指向整型数据的指针变量 int a[n]: 定义整形数组a,他有n个元素 int *p[n]: 定义指针数组p,它有n个指向整型数据的指针元素组 ...