Spring深入浅出(一)IOC的基本知识
Spring前言
Spring是一个企业级开发框架,为解决企业级项目开发过于复杂而创建的,框架的主要优势之一就是分层架构,允许开发者自主选择组件。
Spring的两大核心机制是IOC(控制反转)和AOP(面向切面编程),从开发的角度讲,我们使用Spring框架就是用它的IOC和AOP。
IOC是典型的工厂模式,通过工厂去注入对象。
AOP则是代理模式的体现。
IOC控制反转
IOC也叫控制反转,首先从字面意思理解,什么叫控制反转?反转的是什么?
在传统的程序开发中,需要调用对象时,通常由调用者来创建被调用者的实例,即对象是由调用者主动使用new关键字出来的。
但在Spring框架中创建对象的工作不再由调用者来完成,而是交给IOC容器来创建,再推送给调用者,整个流程完成反转,所以是控制反转。也就是
例如:买礼品。
传统方式:你去礼品店,需要自己去挑,然后自己带回来。
IOC:你只需要把需要的东西的清单放在家门口,然后就像圣诞老人一样将礼物放到你们的门口,你可以直接拿到用就行。
IOC的代码描述与解释
通过传统方式创建并且实例化一个对象
1.传统模式去生成一个人的对象,有属性和set/get方法
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.在测试方法中去实例化Person对象
public class Test {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p);
}
}
通过Spring的方式去创建并且实例化一个对象
准备工作:
1.添加Spring相关jar包。
2.创建配置文件,可以自定义文件名applicationContext.xml。
3.调用API。
<?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"> <!-- 配置Person对象-->
<!--
1.id:对象名。
2.class:实体类的全路径名
-->
<bean id="p" class="com.test.entity.Person"></bean> </beans>
public class Test {
public static void main(String[] args) {
//1.加载spring的配置文件,生成ApplicationContext对象。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.调用ApplicationContext的getBean方法获取对象,通过在配置文件中的bean的id值获取对象
Person p = (Student) applicationContext.getBean("p");
System.out.println(p);
}
}
程序在加载spring的配置文件的时候创建person对象,通过反射机制调用无参构造函数,所以要求交给IOC容器管理的类必须有无参构造函数。此时的person对象的属性全部为空,因为调用无参构造只会创建对象而不会进行赋值,如何赋值呢?
给实体类赋初始值(无参构造的方式)
添加property标签:name对应实体类中的属性名,value是需要赋予属性的初始值。可以在property标签中使用其属性名,也可以在property中使用value标签
注意: (1)若包含特殊字符,比如 name="<ZhangSan>",需要使用<![CDATA[<ZhangSan>]]>进行配置。
(2)Spring是通过调用每个属性的setter方法来完成属性的赋值。所以实体类必须有setter方法,否则加载时报错,getter方法可写可不写。
<?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"> <!-- 配置Person对象-->
<!--
1.id:对象名。
2.class:实体类的全路径名
--> <bean id="p" class="com.test.entity.Person">
<property name="id" value="1"></property>
<property name="name">
<value><![CDATA[<ZhangSan>]]></value>
</property>
<property name="name" value="zhangsan"></property>
</bean> </beans>
调用方法与上面一样,就不再描述了。需要注意的如下:
//1、第一种方式,使用配置文件中的id,这也是最常用的方式
Person p = (Student) applicationContext.getBean("p"); //2、第二种方式,直接使用类名,但是如果在配置文件中有两个bean都是由Student类生成的,IOC容器无法将两个bean都返回,必须指定一个唯一的bean。因此不推荐使用
Person p = (Student) applicationContext.getBean(Person.class);
给实体类赋初始值(有参构造的方式)
1.创建一个实体类
2.在配置文件中的属性写法
3.实例化
//实体类
public class Person {
private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(int id, String name,) {
super();
this.id = id;
this.name = name;
}
}
<?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"> <!-- 通过有参构造函数创建对象 --> <!--
1.此时IOC容器会根据constructor-arg标签去加载对应的有参构造函数,创建对象并完成属性赋值。
name的值需要与有参构造的形参名对应,value是对应的值。
-->
<bean id="p" class="com.test.entity.Person">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="ZhangSan"></constructor-arg>
</bean> <!--
2.除了使用name对应参数外,还可以通过下标index对应。
index的值必须和实体类中的顺序一一对应
-->
<bean id="p2" class="com.test.entity.Person">
<constructor-arg index="0" value="2"></constructor-arg>
<constructor-arg index="2" value="LiSi"> </constructor-arg>
</bean> </beans>
IOC容器管理多个对象
//创建一个班级类
public class Classes {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} //创建一个学生类
public class Student {
private int id;
private String name;
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
<?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"> <!-- 创建classes对象 -->
<bean id="classes" class="com.hzit.entity.Classes">
<property name="id" value="1"></property>
<property name="name" value="Java班"></property>
</bean> <!-- 创建stu对象 -->
<bean id="stu" class="com.hzit.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
<!-- 将classes对象赋给stu对象 使用ref标签(引用)-->
<property name="classes" ref="classes"></property>
</bean> </beans>
在spring的配置文件中,通过ref属性将其他bean赋给当前bean对象,这种方式叫做依赖注入(DI),DI也是Spring中IOC的重要一环,所以一般说的IOC包括DI。
在实体类中包含Array,List,Map的赋值
public class Classes {
private int id;
private String name;
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?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"> <!-- 配置classes对象 -->
<bean id="classes" class="com.hzit.entity.Classes">
<property name="id" value="1"></property>
<property name="name" value="Java班"></property>
<property name="students">
<!-- 注入student对象 -->
<list>
<ref bean="stu"/>
<ref bean="stu2"/>
</list>
</property>
</bean> <bean id="stu" class="com.hzit.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="Zhangsan"></property>
</bean> <bean id="stu2" class="com.hzit.entity.Student">
<property name="id" value="2"></property>
<property name="name" value="LiSi"></property>
</bean> </beans>
上述描述了集合属性通过list标签和ref标签完成注入(数组与list一样)。
ref的bean属性指向需要注入的bean对象。
Set,Map,Properties等集合的方式与list相同,只是标签不同,分别是<set></set>,<map></map>,<props></props>
总结
IOC是Spring的核心,很重要。使用Spring开发项目时,控制层,业务层,DAO层都是通过IOC来完成依赖注入(DI)的。
Spring深入浅出(一)IOC的基本知识的更多相关文章
- Spring Ioc源码分析系列--Ioc的基础知识准备
Spring Ioc源码分析系列--Ioc的基础知识准备 本系列文章代码基于Spring Framework 5.2.x Ioc的概念 在Spring里,Ioc的定义为The IoC Containe ...
- Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载
IOC的单例模式--Bean Spring中的bean是根据scope来决定的. scope有4种类型: 1.singleton:单例模型,表示通过Spring容器获取的该对象是唯一的.常用并且默认. ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Spring系列之IOC的原理及手动实现
目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
- Spring入门1. IoC入门实例
Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...
- Spring中的IOC
在学习spring的时候,最常听到的词应该就是IOC和AOP了,以下,我从我的角度再次理解一下Spring里的IOC和AOP. IOC简单介绍 IoC(InversionofControl):IoC就 ...
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- 用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)
用IDEA详解Spring中的IoC和DI 一.Spring IoC的基本概念 控制反转(IoC)是一个比较抽象的概念,它主要用来消减计算机程序的耦合问题,是Spring框架的核心.依赖注入(DI)是 ...
- 依赖注入(DI)与控制反转(IOC)基础知识
依赖注入(DI)与控制反转(IOC)基础知识 一.什么是依赖注入? 依赖注入英文是Dependcy Injection简写DI,依赖注入会将所依赖的对象自动交由目标对象使用,而不是让对象自己去获取. ...
随机推荐
- jms及active(jdk api)的实现
在企业中,分布式的消息队列需要实现的问题: 1.不同的业务系统分别处理同一个消息(订阅发布),同一个业务系统负载处理同一类消息(队列模式) 2.消息的一致性问题,在互联网公司中一般不要求强一致性,一般 ...
- 【HDOJ 5407】 CRB and Candies (大犇推导
pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...
- 棋盘覆盖问题python3实现
在2^k*2^k个方格组成的棋盘中,有一个方格被占用,用下图的4种L型骨牌覆盖全部棋盘上的其余全部方格,不能重叠. 代码例如以下: def chess(tr,tc,pr,pc,size): globa ...
- bzoj3173: [Tjoi2013]最长上升子序列(树状数组+二分倒推)
3173: [Tjoi2013]最长上升子序列 题目:传送门 题解: 好题! 怎么说吧...是应该扇死自己...看错了两次题: 每次加一个数的时候,如果当前位置有数了,是要加到那个数的前面,而不是直 ...
- m_Orchestrate learning system---十五、如何快速查错
m_Orchestrate learning system---十五.如何快速查错 一.总结 一句话总结: a.删除代码法 b.添加提示代码法 c.仔细看错误信息 1.评论板块和论坛板块的实时更新? ...
- flex布局下img变形的问题
flex-shrink 加上:flex-shrink:0:定义了缩小比例,默认为1,即如果空间不足,项目将会缩小所有项目为1时,空间不足,都会缩小,如果你不希望某个容器在任何时候都不被压缩,那设置f ...
- Windows下安装和使用MongoDB
支持平台:从2.2版本开始,MongoDB不再支持Windows XP.要使用新版本的MongoDB,请用更新版本的Windows系统. 重要:如果你正在使用Windows Server 2008 R ...
- 读 Real-Time Rendering 收获 - chapter 6. texturing
Texturing, at its simplest, is a techinique for efficiently modeling the surface's properties.
- 前端压缩图片,前端压缩图片后转换为base64.
今天利用一上午研究了一下前端如何将5m左右的照片转换base64大小为 100k以内! 有两个链接:https://www.cnblogs.com/007sx/p/7583202.html :http ...
- 虚拟机创建后该如何获取IP地址并访问互联网实用教程
之前在做项目的时候主机IP地址.网关.DNS.子网掩码等都是公司或者对方直接给提供的,但是如果我们自己想搭建一台虚拟机或者一台集群的话,手头又没有IP地址,该肿么办呢? 白慌,这里介绍一个小技巧, ...