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个对象组成的,所有的对象通过彼此的合作, ...
随机推荐
- 第五周 Spring框架
一.Spring框架设计 Spring framework 6大模块 1.1 Spring AOP AOP: 面向切面编程 Spring 早期版本的核心功能,管理对象声明周期和对象装配 为了实现管理和 ...
- 这款拓展让你的jupyter lab更高效
有一段时间没有分享过有关jupyter lab的内容了,今天给大家介绍一款实用的jupyter lab插件,可以帮助我们打造更灵活易用的jupyter lab. 图1 这款拓展的名称叫做jlab-en ...
- Shiro-JWT SpringBoot前后端分离权限认证的一种思路
JWT-Shiro 整合 JWT-与Shiro整合进行授权认证的大致思路 图示 大致思路 将登录验证从shiro中分离,自己结合JWT实现 用户登陆后请求认证服务器进行密码等身份信息确认,确认成功后 ...
- 深入理解 sync.Once 与 sync.Pool
深入理解 sync.Once 与 sync.Pool sync.Once 代表在这个对象下在这个示例下多次执行能保证只会执行一次操作. var once sync.Once for i:=0; i & ...
- 手写Spring,定义标记类型Aware接口,实现感知容器对象
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 同事写的代码,我竟丝毫看不懂! 大佬的代码,就像 "赖蛤蟆泡青蛙,张的丑玩 ...
- Vue3全局APi解析-源码学习
本文章共5314字,预计阅读时间5-15分钟. 前言 不知不觉Vue-next的版本已经来到了3.1.2,最近对照着源码学习Vue3的全局Api,边学习边整理了下来,希望可以和大家一起进步. 我们以官 ...
- 精尽Spring Boot源码分析 - Jar 包的启动实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Kubernetes之deployment
Kubernetes实现了零停机的升级过程.升级操作可以通过使用ReplicationController或者ReplicaSet实现,但是Kubernetes提供了另一种基于ReplicaSet的资 ...
- [Django REST framework - 视图组件之视图基类、视图扩展类、视图子类、视图集]
[Django REST framework - 视图组件之视图基类.视图扩展类.视图子类.视图集] 视图继承关系 详图见文章末尾 视图组件可点我查看 两个视图基类:APIView.GenericAP ...
- Linux中su和sudo的用法
su -#su - oldboy //当执行这个命令的时候表示切换到oldboy用户,并且重新读取用户环境相关配置文件,具体的来说就是执行下用户家目录下.bash_profile和.bashrc文件, ...