Spring IOC容器(2)

x33g5p2x  于2021-08-23 转载在 Spring  
字(6.2k)|赞(0)|评价(0)|浏览(319)

一 IOC和 Bean介绍

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

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

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

二 container 总览

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

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

当你的对象和配置元数据完成之后,ApplicationContext 会初始化和创建,然后你就可以完全执行系统或者应用,如下图:
在这里插入图片描述

三 初识配置元数据

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

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

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

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

<?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目录的子目录。

<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<T> 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 输出结果

在这里插入图片描述

相关文章