Spring IOC介绍

Spring IOC介绍,第1张

文章目录
  • 🍀Spring IOC介绍
    • 🌵IOC是什么
    • ☘️IOC能做什么
    • 🪴Spring容器管理对象Demo
    • 🌱Spring中IOC容器介绍
    • 🌾ApplicationContext常见实现类
    • 🌿容器管理对象过程

🍀Spring IOC介绍 🌵IOC是什么

IOC(Inversion of Control)即“控制反转”,意味着将设计好的对象交给容器控制。

传统的Java程序的设计,直接在对象内部通过new形式创建对象,是程序主动去创建依赖对象,而IOC技术是专门一个容器来创建对象,IOC容器来控制对象的创建而不是显式的使用new。

  • 对象的创建交给容器管理,这就是控制反转。

  • 控制反转是通过容器来完成的,Spring中提供了这样一个容器,称之为IOC容器。

  • 无论是创建对象,处理对象之间的依赖,对象的创建时间还是对象创建的数量,都是由Spring为我们提供的IOC容器上的配置上兑现信息就行。

☘️IOC能做什么

有了IOC容器之后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松耦合的,这样方便进行测试,利用功能复用,使整个体系结构变动非常灵活。

由IOC容器来帮对象找相应的依赖对象并注入,而不是对象主动去找。核心点在于,资源不是由使用资源的的调用方管理,而是不使用资源的第三方管理,第三方指的就是IOC容器。

IOC优势

资源的集中管理,实现资源的可配置和易管理
降低了使用资源双方的依赖程度,也就是说的松耦合

🪴Spring容器管理对象Demo

1.引入spring的相关依赖


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-coreartifactId>
    <version>4.3.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>4.3.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.3.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-expressionartifactId>
    <version>4.3.5.RELEASEversion>
dependency>

2.添加spring配置文件




<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-3.0.xsd">

beans>

3.创建实体类

public class Student {
    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;
    }
}

4.通过IOC容器管理对象

以前通过new方式创建Student对象。
Student student = new sStudent();

现在通过IOC容器,可以让IOC负责创建并管理对象,直接在applicationcontext.xml配置文件中配置对应信息。

5.通过容器获取Student对象

public class TestIOC {
    public static void main(String[] args) {
        String path = "applicationContext1.xml";
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
        Student student = (Student)applicationContext.getBean("student");
        System.out.println(student);
    }
}

执行结果:

以上执行结果可知:可以通过容器来获取对象实例

总结: Spring IOC *** 作总结

  1. 增加一个spring配置文件
  2. 对xml文件进行解析
  3. 获取BeanFactory工厂类
  4. 在工厂类的方法中使用反射创建bean对象
🌱Spring中IOC容器介绍


以上是Spring IOC容器中接口的继承关系,其中ApplicationContext接口是BeanFactory的子接口。

  • BeanFactory是Spring IOC容器所定义的最底层接口,提供了比较先进的配置机制,使得任何对象的配置成为可能。
  • ApplicationContext是BeanFactory的最高级实现之一,并在BeanFactory功能上做了很多扩展,所以在大部分的工厂场景下,都会使用ApplicationContext作为IOC容器。

ApplicationContext在BeanFactory区别:

①BeanFactory的实现是按需创建,即第一次获取bean时才创建bean。
②ApplicationContext会一次性创建所有的bean,ApplicationContext也提供了一些额外的功能,比如与Spring的AOP更容易继承,也提供了Message Resource的机制(用于处理国际化支持)和通知机制。

🌾ApplicationContext常见实现类
  • ClassPathXmlApplicationContext

读取classpath,如果配置文件在当前系统类路径下,可以优先使用ClassPathXmlApplicationContext类

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationcontext.xml”);

  • FileSystemXmlApplicationContext

读取指定路径下的资源,如果配置文件放在文件系统路径下,则可以优先选用FileSystemXmlApplicationContex

FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("d://application.xml");

  • XmlWebApplicationContext

① XmlWebApplicationContext ac = new XmlWebApplicationContext(); // 这时并没有初始化容器
② ac.setServletContext(servletContext); // 需要指定ServletContext对象
③ ac.setConfigLocation(“/WEB-INF/applicationContext.xml”); // 指定配置文件路径,开头的斜线表示Web应用的根目录
④ ac.refresh(); // 初始化容器

🌿容器管理对象过程

  1. Spring启动时读取应用程序提供Bean配置信息,并在Spring容器中生成一份相应的Bean配置注册表;

  2. 根据这张注册表实例化Bean;

  3. 通过配置文件装配好Bean之间的依赖关系,放到Bean缓存池,为上层应用提供准备就绪的运行环境。

Bean缓存池:通过hashMap实现


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719629.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存