Spring总结之IOC
一、Spring IOC 简介
IOC(Inverse of control):控制反转,又称作依赖注入,主要是把创建对象和查找依赖对象的控制权交给IOC容器,由IOC容器管理对象的生命周期,是一种重要的面向对象编程的法则来消减计算机程序的耦合问题,是Spring框架的核心。
1、IOC例子说明
业务场景:员工工作,当有一份工作出现时,会有对应的人来做此工作,如果在工作中直接new出员工,这样工作和员工就绑定到一起了,然而正确的场景却不希望这样,工作只负责工作,人员应该由主管来确定。
例子:工作与员工绑定到一起了
//员工张三
public class Zhangsan { public void doWork() {
System.out.println("张三开发");
}
} //工作业务逻辑
public class DoWork { public void doWork() {
Zhangsan zs = new Zhangsan();
zs.doWork();
}
} public class SpringTest {
//开发主管
@Test
public void zhuguan() {
DoWork dw = new DoWork();
dw.doWork()1;
}
}
例子:工作安排控制权交给主管
//员工逻辑
public interface Employee {
void doWork();
}
//员工张三
public class Zhangsan implements Employee{ public void doWork() {
System.out.println("张三开发");
}
}
//工作业务逻辑
public class DoWork { private Employee user; public void setUser(Employee user) {
this.user = user;
} public void doWork() {
user.doWork();
}
}
public class SpringTest {
//开发主管
@Test
public void zhuguan() {
DoWork dw = new DoWork();
dw.setUser(new Zhangsan());
dw.doWork();
}
}
这个控制由那位员工来开发权利可以交由spring控制,这样工作业务代码和员工就解耦了:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="zhangsan" class="top.ruandb.entity.Zhangsan"></bean>
<bean id="doWork" class="top.ruandb.service.DoWork">
<property name="user" ref="zhangsan"></property>
</bean>
</beans>
public class SpringTest { ApplicationContext ac ; @Before
public void setUp() {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test1() {
DoWork doWork = (DoWork) ac.getBean("doWork");
doWork.doWork();
}
@After
public void tearDown() {
ac = null;
}
}
2、依赖注入
依赖注入其实就是控制反转的另一种说法,获得依赖对象的方式被反转了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 属性注入 -->
<bean id="people" class="top.ruandb.entity.People">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
</bean>
<!-- 构造函数注入;(通过类型) -->
<bean id="people1" class="top.ruandb.entity.People">
<constructor-arg type="String" value="李四"></constructor-arg>
<constructor-arg type="int" value="18"></constructor-arg>
</bean>
<!-- 构造函数注入;(通过索引) -->
<bean id="people2" class="top.ruandb.entity.People">
<constructor-arg index="0" value="王五"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
</bean>
<!-- 构造函数注入;(类型和索引联合使用) -->
<bean id="people3" class="top.ruandb.entity.People">
<constructor-arg type="String" index="0" value="赵六"></constructor-arg>
<constructor-arg type="int" index="1" value="18"></constructor-arg>
</bean>
<!-- 工厂注入(非静态工厂) -->
<bean id="peopleFactory" class="top.ruandb.factory.PeopleFactory"></bean>
<bean id="people4" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<!-- 工厂注入(静态工厂) -->
<bean id="people5" class="top.ruandb.factory.PeopleFactory2" factory-method="createPeople"></bean>
<!-- 注入参数 -->
<bean id="dog" class="top.ruandb.entity.Dog">
<property name="name" value="tom"></property>
</bean>
<bean id="people6" class="top.ruandb.entity.People">
<!-- 基本类型值 -->
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<!-- 注入 bean参数(ref方式) -->
<property name="dog" ref="dog"></property>
<!-- 注入 bean参数(内部bean方式) -->
<property name="cat">
<bean class="top.ruandb.entity.Cat">
<property name="name" value="lucy"></property>
</bean>
</property>
<!-- 注入null -->
<property name="nullString">
<null></null>
</property>
<!-- 注入List -->
<property name="hobbys">
<list>
<value>吃饭</value>
<value>睡觉</value>
</list>
</property>
<!-- 注入Set -->
<property name="loves">
<set>
<value>上班</value>
<value>加班</value>
</set>
</property>
<!-- 注入Map -->
<property name="map">
<map>
<entry>
<key><value>1</value></key>
<value>2</value>
</entry>
<entry>
<key><value>3</value></key>
<value>4</value>
</entry>
</map>
</property>
</bean>
<!-- Spring默认的bean都是单例,可以通过配置 prototype ,实现多例.其他类通过某方法获取多例,方法注入 lookup-method -->
<bean id="dog1" class="top.ruandb.entity.Dog" scope="prototype"></bean>
<bean id="people6" class="top.ruandb.entity.People">
<lookup-method name="getDog" bean="dog1"/>
</bean>
<!-- bean关系 继承:parent;依赖:depends-on(依赖后会先初始化依赖的类);引用:ref -->
<bean id="abstractPeople" class="top.ruandb.entity.People" abstract="true">
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean>
<bean id="zhangsan" parent="abstractPeople" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<!-- 引用dog -->
<property name="dog" ref="dog"></property>
</bean>
<bean id="autority" class="top.ruandb.entity.Authority"></bean> <!-- bean的作用范围 scope
1,singleton Spring ioc 容器中仅有一个 Bean 实例,Bean 以单例的方式存在;
2,prototype 每次从容器中调用 Bean 时,都返回一个新的实例;
3,request 每次 HTTP 请求都会创建一个新的 Bean;
4,session 同一个 HTTP Session 共享一个 Bean;
5,global session 同一个全局 Session 共享一个 Bean,一般用于 Portlet 应用环境;
6,application 同一个 Application 共享一个 Bean;
-->
</beans>
Spring总结之IOC的更多相关文章
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Spring学习之Ioc控制反转(2)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- 比Spring简单的IoC容器
比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring框架之IOC(控制反转)
[TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...
- 一) Spring 介绍、IOC控制反转思想与DI依赖注入
一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...
- Spring系列之IOC的原理及手动实现
目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...
- Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)
Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
随机推荐
- 行人检测与重识别!SOTA算法
行人检测与重识别!SOTA算法 A Simple Baseline for Multi-Object Tracking, Yifu Zhang, Chunyu Wang, Xinggang Wang, ...
- YOLOV4各个创新功能模块技术分析(二)
YOLOV4各个创新功能模块技术分析(二) 四.数据增强相关-GridMask Data Augmentation 论文名称:GridMask Data Augmentation 论文地址:https ...
- 简化可视SLAM应用程序的开发
简化可视SLAM应用程序的开发 Easing the development of visual SLAM applications 同步定位和映射(SLAM)描述了一个设备(如机器人)使用传感器数据 ...
- Springboot-Redis分布式锁 -----StringRedisTemplate
这里引用别人, 用来自己回忆 https://blog.csdn.net/jack_shuai/article/details/91986690 https://www.cnblogs.com/mox ...
- javaBean命名规范 get / set 后的首字母大写
javaBean命名规范 Sun 推荐的命名规范 1 ,类名要首字母大写,后面的单词首字母大写 2 ,方法名的第一个单词小写,后面的单词首字母大写 3 ,变量名的第一个单词小写,后面的单词首字母大写 ...
- 【NX二次开发】获得屏幕矩阵并设置WCS为屏幕方向
说明:获得屏幕矩阵并设置WCS为屏幕方向(Z朝向自己,X轴朝右,Y轴超上). 方法: 1 extern DllExport void ufusr(char *param, int *retcode, ...
- Django基础之路由层
内容概要 路由匹配 无名有名分组 反向解析 无名有名分组反向解析(难理解) 路由分发 名称空间 伪静态 内容详细 1 路由匹配 urls.py url()方法第一个参数其实是一个正则表达式 第一个参数 ...
- DOS命令行(11)——更多实用的命令行工具
start 启动另一个窗口运行指定的程序或命令,所有的DOS命令和命令行程序都可以由start命令来调用.该命令不仅能运行程序,还能运行协议对应的程序 命令格式:START ["title& ...
- 从 html 实现一个 react🎅
前言 我们认为,React 是用 JavaScript 构建快速响应的大型 Web 应用程序的首选方式.它在 Facebook 和 Instagram 上表现优秀.官网地址 react 的理念是在于对 ...
- 28、python3.7(windows)将ORACLE11gR2中的数据取出写入excel表
28.1.下载python的离线扩展模块: 1.windows下python的离线扩展模块下载地址为: https://www.lfd.uci.edu/~gohlke/pythonlibs/ 提示: ...