spring framework之IoC

作者: 机灵鬼鬼 | 来源:发表于2022-02-09 17:00 被阅读0次

spring官方文档:版本Version 5.3.15
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans

image.png

ioc容器的基础包

org.springframework.beans
org.springframework.context

ioc特点

1、更容易与Spring的AOP功能集成
2、消息资源处理(用于国际化)
3、事件发布
4、特定于应用层的上下文,XXApplicationContext

BenFactory

BeanFactory是容器的基础工厂接口,它有以下很多扩展的XXFactory工厂和XXApplicationContext
image.png

ApplicationContext

它的包路径

org.springframework.context.ApplicationContext
ApplicationContext是BeanFactory的完整超集,在本章中专门用于描述Spring的IoC容器,
你也可以简单理解Spring IoC容器就是ApplicationContext。
ApplicationContext接口代表Spring IoC容器,负责实例化、配置和组装bean。
容器通过读取配置元数据来获取关于实例化、配置和组装哪些对象的指令。
配置元数据以XML、Java注释或Java代码,它有很多实现拓展,我们这里统称为XXApplicationContext。
image.png

问题来了,容器有了,怎么往容器里加料(bean等资源)呢?

基于容器的配置
两种方式,第一种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"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <context:annotation-config/>

</beans>

说明:
这个配置 <context:annotation-config/> 隐式地向容器中注册了一下几种XXProcessor

Resource(资源)

如何从系统中加载资源进入ioc容器?spring framework提供了一个Resource接口,其中有很多方法。

public interface Resource extends InputStreamSource {

    boolean exists();

    boolean isReadable();

    boolean isOpen();

    boolean isFile();

    URL getURL() throws IOException;

    URI getURI() throws IOException;

    File getFile() throws IOException;

    ReadableByteChannel readableChannel() throws IOException;

    long contentLength() throws IOException;

    long lastModified() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    String getFilename();

    String getDescription();
}

同时spring里有几个内置的接口实现

  • UrlResource
    使用场景:网URL和可用于访问通常可通过URL访问的任何对象,例如文件、HTTPS目标、FTP目标等

  • ClassPathResource
    此类表示应从类路径获取的资源。它使用线程上下文类加载器、给定类加载器或给定类来加载资源。

  • FileSystemResource
    支持Path同时支持URL,即同时支持java.io.File和java.nio.file.Path处理器。

  • PathResource
    这是一个基于java.nio.file.Path处理器的实现,所有的操作和处理都是基于Path API。

  • ServletContextResource
    这是ServletContext资源的资源实现,它解释相关web应用程序根目录中的相对路径。

  • InputStreamResource
    InputStreamResource是给定InputStream的资源实现。只有在没有具体的资源实现适用的情况下才应该使用它。特别是,在可能的情况下,首选ByteArrayResource或任何基于文件的资源实现。

与其他资源实现不同,这是一个已打开资源的描述符。因此,它从isOpen()返回true。如果需要将资源描述符保留在某个位置,或者需要多次读取流,请不要使用它。

ResourceLoader接口

public interface ResourceLoader {

    Resource getResource(String location);

    ClassLoader getClassLoader();
}
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

针对ClassPathXmlApplicationContext,该代码返回ClassPathResource。如果对FileSystemXmlApplicationContext实例运行相同的方法,它将返回一个FileSystemResource。对于WebApplicationContext,它将返回ServletContextResource。它同样会为每个上下文返回适当的对象。

相关文章

网友评论

    本文标题:spring framework之IoC

    本文链接:https://www.haomeiwen.com/subject/oswmkrtx.html