Spring IOC (Inversion Of Control反转控制容器

一、对于IOC容器的简单理解

在java开发中将程序中的对象交给容器管理,而不是在对象的内部管理。

那么两个简单的问题去分析理解IOC容器

1.为什么叫反转:

IOC容器实现了将对象的管理由对象内部直接管理(比如在一个类的main方法中new了一个String对象)转换成在容器中被动的被注入,创建。把这种对对象管理方式的改变称为反转。

2.控制了什么:

IOC容器控制了外部资源的统一获取(不仅仅包括对象,还有其他的文件)

(敲下黑板:spring容器是IOC容器,但是IOC容器不是spring容器,因为基于IOC容器设计的框架不仅只有spring)

二、应用上下文

现在我们手上有了一个魔杖(IOC容器)但是我们还需要对应的魔咒(应用上下文)来驱动魔杖

spring容器有两种实现:

1.最基础的BeanFactory(实体工厂)由于功能过于简单所以不经常使用

2.由BeanFactory派生的多种应用上下文,其抽象接口是ApplicationContext,spring根据应用场景的不同给我们设计了几种应用上下文:

(1) AnnotationConfigApplicationContext:从一个或多个基于java的配置类中加载上下文定义,适用于java注解的方式;

(2)ClassPathXmlApplicationContext:从类路径下的一个或多个xml配置文件中加载上下文定义,适用于xml配置的方式;

(3)FileSystemXmlApplicationContext:从文件系统下的一个或多个xml配置文件中加载上下文定义,也就是说系统盘符中加载xml配置文件;

(4)AnnotationConfigWebApplicationContext:专门为web应用准备的,适用于注解方式;

我们在这里使用的是ClassPathXmlApplication,xml配置方式

三、简单的实现IOC控制对象

1.创建一个应用上下文applicationContext_ioc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<beans>

2.创建一个类Student

package spring.ioc.unities;

public class Student {
private String stuNo;
private String stuName;
private int gender;
private String nativePlace;
private String classNo;
private String tel;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getNativePlace() {
return nativePlace;
}
public void setNativePlace(String nativePlace) {
this.nativePlace = nativePlace;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", gender=" + gender + ", nativePlace="
+ nativePlace + ", classNo=" + classNo + ", tel=" + tel + "]";
} }

3.在xml中通过bean标签俩控制Bean

(1)标签中必要的两个属性

id(用于识别ioc容器中的bean)

class(id对应的类的位置,通常是包名+类名,建议直接复制,手写有可能出错)

<bean id="stu1" class="spring.ioc.unities.Student"></bean>  

(2)标签中其他的属性

scope属性:

 作用域  说明
 Singleton

Spring IOC容器中一个bean定义只有一个对象实例,默认情况下会在容器启动时初始化

 Prototype 每次从容器获取bean都是新的对象 
 Request  每次Http请求都会创建一个新的bean,只使用在WebApplicationContext中
 Session  类似request,每次有新的会话都会创建一个新的Bean,只使用在WebApplicationContext中

3.main函数中测试确认:

public static void main(String[] args) {

        ApplicationContext ioc =  new ClassPathXmlApplicationContext("applictionContext_ioc.xml");
Student stuA = (Student) ioc.getBean("stu1");
Student stuB = (Student) ioc.getBean("stu1");
//在ioc中获取的bean:
System.out.println("获取的属性为singleton的bean-------------------------");
System.out.println(stuA.toString()+stuB.toString());
//验证A,B是否是同一个实体
System.out.println("stua和stub是否是一个bean:"+(stuA==stuB));
Student stuC = (Student) ioc.getBean("stu2");
Student stuD = (Student) ioc.getBean("stu2");
System.out.println("获取的属性为prototype的bean-------------------------");
System.out.println(stuC.toString()+stuD.toString());
System.out.println("stuc和stud是否是一个bean:"+(stuC==stuD)); }

获得结果如下:

可以看出singleton属性的bean其实是单例模式下的bean.

Spring IOC 的简单使用的更多相关文章

  1. Spring IOC的简单实现

    简单的说,Spring就是通过工厂+反射将我们的bean放到它的容器中的,当我们想用某个bean的时候,只需要调用getBean("beanID")方法即可. 原理简单说明: Sp ...

  2. Spring IOC 方式结合TESTGN测试用例,测试简单java的命令模式

    java命令模式: 可以命令("请求")封装成一个对象,一个命令对象通过在特定的接收着上绑定一组动作来封装一个请求.命令对象直接把执行动作和接收者包进对象中,只对外暴露出执行方法的 ...

  3. 1.Spring IoC简单例子

    Spring IoC简单例子 1.IHelloMessage.java package com.tony.spring.chapter01; public interface IHelloMessag ...

  4. Spring容器的简单实现(IOC原理)

    引言:容器是什么?什么是容器?Spring容器又是啥东西?我给Spring容器一个对象名字,为啥能给我创建一个对象呢? 一.容器是装东西的,就像你家的水缸,你吃饭的碗等等. java中能作为容器的有很 ...

  5. Spring IOC 源码简单分析 03 - 循环引用

    ### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java   ioc03. ...

  6. Spring IOC 源码简单分析 02 - Bean Reference

    ### 准备 ## 目标 了解 bean reference 装配的流程 ##测试代码 gordon.study.spring.ioc.IOC02_BeanReference.java   ioc02 ...

  7. (反射+内省机制的运用)简单模拟spring IoC容器的操作

    简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...

  8. spring IOC简单分析

    Spring IOC 体系结构 BeanFactory(BeanFactory 里只对 IOC 容器的基本行为作了定义,根本不关心你的 bean 是如何定义怎样加载的.正如我们只关心工厂里得到什么的产 ...

  9. Spring IOC 源码简单分析 04 - bean的初始化

      ### 准备 ## 目标 了解 Spring 如何初始化 bean 实例 ##测试代码 gordon.study.spring.ioc.IOC04_Initialization.java publ ...

随机推荐

  1. pycharm的使用(day03复习整理)

    pycharm的使用 file --> settings --> editor -->general --> change font size .... file --> ...

  2. (二)AS给button添加点击事件

    三种方法给Button添加点击事件 (一)通过button的id,添加继承View.OnClickListener的监听实现 <Button android:id="@+id/btn_ ...

  3. unity image 设置图片

    从任意文件目录下读取文件并在unity中显示: 1)读取目标文件 byte[] imageByte = File.ReadAllBytes(imagePath); 2)转换成纹理 texture.Lo ...

  4. django-URL之include函数(五)

    三种格式:(1)incude(module,namespace=None) from django.urls import path,include from book import urls url ...

  5. PHP 类中使用全局变量和全局常量

    <?php $global_var = "var"; define('global_const', 'const'); class Test { public $_var; ...

  6. 给自己网站配置 https,http2 ,gzip压缩

    https 需要购买域名ssl证书 注意事项: 1.要开启HTTP/2协议支持,需要在nginx 1.10以上版本并且需要openssl库的版本在1.0.2及以上编译. 2.http2.0只支持开启了 ...

  7. 20190906_matplotlib_学习与快速实现

    20190906 Matplotlib 学习总结 第一部分: 参考连接: Introduction to Matplotlib and basic line https://www.jianshu.c ...

  8. python基础-字符串(str)类型及内置方法

    字符串-str 用途:多用于记录描述性的内容 定义方法: # 可用'','''''',"","""""" 都可以用于定义 ...

  9. 第三十二章 System V信号量(三)

    n哲学家进餐问题描述有五个哲学家,他们的生活方式是交替地进行思考和进餐,n哲学家们共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五支筷子,n平时哲学家进行思考,饥饿时便试图取其左.右最靠近 ...

  10. 前端技术之:webpack热模块替换(HMR)

    第一步:安装HMR中间件: npm install --save-dev webpack-hot-middleware   第二步:webpack配置中引入webpack对象     const we ...