一、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的更多相关文章

  1. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

  2. Spring学习之Ioc控制反转(2)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

  3. 比Spring简单的IoC容器

    比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...

  4. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  5. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  6. Spring框架之IOC(控制反转)

    [TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...

  7. 一) Spring 介绍、IOC控制反转思想与DI依赖注入

    一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...

  8. Spring系列之IOC的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...

  9. Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

    Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...

  10. Spring框架中IoC(控制反转)的原理(转)

    原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...

随机推荐

  1. ADAS虚拟车道边界生成

    ADAS虚拟车道边界生成 Virtual Lane Boundary Generation for Human-Compatible Autonomous Driving: A Tight Coupl ...

  2. 单点突破:MySQL之索引

    前言 开发环境:MySQL5.7.31 什么是索引 在MySQL中,索引(Index)是帮助高效获取数据的数据结构. 我们可以将数据库理解为一本书,数据库中的各个数据列(column)就是目录中的章节 ...

  3. 总结springboot开启mybatis驼峰命名自动映射的三种方式

    方式一:通过springboot的配置文件application.yml mybatis: configuration: map-underscore-to-camel-case: true 此方式是 ...

  4. jmeter--JSON Extractor 用法

    JMeter处理大部分请求返回的结果,都是json.对于请求返回的结果,处理以后作为其他请求的参数,有一个方便使用的插件:JSON Extractor JSON Extractor中文叫做json提取 ...

  5. 04:CSS(02)

    溢出属性 p { height: 100px; width: 50px; border: 3px solid red; /*overflow: visible; !*默认就是可见 溢出还是展示*!*/ ...

  6. 微软官方 Win 11 “体检工具”太烂了?开发者自己做了一个

    1.Win 10 免费升级到 Win 11 最近微软官方终于宣布了 Windows 11,不仅带来了全新的 UI,而且还有很多新功能:比如支持 Android 应用. 虽然微软官方已说明 Win 10 ...

  7. 01 JumpServer安装

    1.0.环境说明: 操作系统类型 主机名称 用户及密码 角色 eth0(Vmnet8) eth1(Vmnet1) 防火墙状态 selinux centos7.4 controlnode root:12 ...

  8. 13、win10系统远程桌面oracle修正问题

    1.A电脑(Windows 10)远程连接B电脑(Widows Server 2016), 出现错误: 出现身份验证错误.要求的函数不受支持 远程计算机:xx.xx.xx.xx 这可能是由于CredS ...

  9. 使用Oracle SQL Developer报错:Unable to find a Java Virtual Machine

    1.环境 win7 x64,oracle 11g r2,jdk6 x64 2.问题 第一次启动Oracle SQL Developer的时候会让我们填写java.exe的路径,我在jdk安装目录下的b ...

  10. 关于asp.net中Repeater控件的一些应用

    在Asp.net中,我是比较喜欢用Repeater这个控件,刚刚遇到的一个问题,怎么实现单击 <asp:LinkButton>,通过后台的单击事件获取同一行数据中的其他数据(对象). 1, ...