Spring IOC 介绍和使用

一 IOC和 Bean介绍

IOC也被称为DI。使用构造器参数,fatory参数,属性的方式的设置对象实例。在这个过程当中建立bean的时候,容器会注入这些依赖,Bean自己经过使用类的直接构造来控制其依赖项的实例化或位置的过程,由于建立Bean的方式完成是反过来的,因此称为Inversion of Control (IoC)。说句人话就是之前建立对象是经过new,如今不new了,直接经过类的构造注入对象。html

org.springframework.beansorg.springframework.context 是 IOC 容器的核心。BeanFactory 接口提供了高级的配置对象的能力;ApplicationContext 是 BeanFactory 的子接口,其额外的功能有AOP特点,消息和资源处理,事件传播等功能,其彻底可以取代BeanFactory,就像父亲是贫农,儿子是高富帅 ;具体的容器好比WebApplicationContext提供了丰富的web 应用功能。java

在spring中,对象是应用的骨架,其被IOC container 所管理,也能够称为 Bean;bean的实例化,组装,和其生命周期都是由 IOC container 管理。Bean和其相互的依赖都是在 配置元数据( configuration metadata)中 配置,而后被IOC容器所管理使用。web

二 container 总览

org.springframework.context.ApplicationContext 其实 表明的就是 IOC container ,它负责 Bean的实例化,组装,和配置。IOC 容器是如何 配置和管理Bean呢?其经过 配置元数据( configuration metadata)的方式得到指示来管理Bean。那么 什么是配置元数据( configuration metadata)呢?configuration metadata 其实就是 XML , java 注解,和java代码。spring

经过少许的配置ApplicationContext就能够开箱即用spring,一般一个单独的应用是会建立ClassPathXmlApplicationContext 或者 FileSystemXmlApplicationContext 实例。尽管 xml 是 一种传统的配置元数据的格式,可是你也可使用少许的xml显示声明的支持java注解和或者Java代码这种元数据格式。在许多应用场景中会建立不少个IOC container 而不是一个。bash

当你的对象和配置元数据完成以后,ApplicationContext 会初始化和建立,而后你就能够彻底执行系统或者应用,以下图: markdown

在这里插入图片描述

三 初识配置元数据

spring 的 配置 最少须要一个或者多个bean,基于xml的配置 <bean> 须要在顶级元素 <beans> 内部,对应的基于java 配置就是 @Bean(用于方法上面) 注解 和 @Configuration(用于类上面)注解。在 <bean> 中 id 表示 bean的惟一标识,class表示bean的全类名,示例以下:app

<?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">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>
复制代码

四 实例化container

提供给ApplicationContext一个或者多个字符串形式的资源路径,ApplicationContext就会经过这个资源路径去加载这些外部 configuration metadata。ui

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
复制代码

<property> 的属性中 name 元素 表示 javaBean的属性,ref指向其它bean的定义。this

<?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">

    <!-- services -->

    <bean id="iocService" class="com.youku1327.ioc.service.IocService">
        <property name="iocDao" ref="iocDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

五 组装xml

在实际开发中业务层和逻辑层是分开的,也就是说一个xml配置bean耦合度过高,咱们须要解耦就须要定义多个mxl,可是,如何在一个xml中引用另外一个xml中的bean呢? 咱们可=能够经过<import/> 元素加载来自其余xml中的bean。在引入外部的xml时,都是当前xml的相对路径,以下示例:services.xml在当前xml同级目录,message.xml在当前xml目录的子目录。spa

<beans>
    <import resource="services.xml"/>
    <import resource="resources/message.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>
复制代码

六使用container

ApplicationContext 是一个高级factory维持着不一样的bean和依赖关系注册表。使用 这个接口的T getBean(String name, Class requiredType) 方法就能得到bean的实例。

6.1 pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

    </dependencies>
复制代码

6.2 dao

/**
 * @Author lsc
 * @Description <p>ioc dao </p>
 * @Date 2019/10/29 20:04
 */
public class IocDao {
}
复制代码

6.3 service

/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/29 20:03
 */
public class IocService {

    private IocDao iocDao;

    private String name;


    public IocDao getIocDao() {
        return iocDao;
    }

    public void setIocDao(IocDao iocDao) {
        this.iocDao = iocDao;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
复制代码

6.4 dao.xml

<?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">

    <!-- services -->

    <bean id="iocDao" class="com.youku1327.ioc.dao.IocDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

6.5 services.xml

<?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">

    <import resource="dao.xml"/>

    <!-- services -->

    <bean id="iocService" class="com.youku1327.ioc.service.IocService">
        <property name="iocDao" ref="iocDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

6.6 application

/**
 * @Author lsc
 * @Description <p> 初始ioc</p>
 * @Date 2019/10/29 22:22
 */
public class Application {

    public static void main(String[] args) {
        // 建立和配置 beans
        ApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"services.xml", "dao.xml"});
        // 得到配置的实例
        IocService service = context.getBean("iocService", IocService.class);
        // 使用配置的实例
        service.setName("youku1327");
        System.out.println(service.getName());
    }
}
复制代码

6.7 输出结果

在这里插入图片描述

七参考文档和公众号

源码在公众号对应文章末尾。

spring reference

在这里插入图片描述

本文同步分享在 博客“知识追寻者”(JueJin)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索