美文网首页
PropertySourceFactory的扩展和使用

PropertySourceFactory的扩展和使用

作者: 单名一个冲 | 来源:发表于2021-03-07 13:48 被阅读0次
  • 扩展点简述

Strategy interface for creating resource-based {@link PropertySource} wrappers.

定义创建资源的PropertySource包装器的策略接口。

  • 扩展点的生命周期及扩展点的执行时机

属于Spring容器创建前,需要先预加载一些属性配置,交由Spring的配置管理器管理,可使用@Value读取。

  • 扩展点的作用

主要用来扩展Spring对资源文件的读取,例如:配置文件等。

  • 扩展点实战

/**
 * 利用Spring自定义的ymal配置读取器
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {

    /**
     * Create a {@link PropertySource} that wraps the given resource.
     *
     * @param name     the name of the property source
     *                 (can be {@code null} in which case the factory implementation
     *                 will have to generate a name based on the given resource)
     * @param resource the resource (potentially encoded) to wrap
     * @return the new {@link PropertySource} (never {@code null})
     * @throws IOException if resource resolution failed
     * @See 解析器 org.springframework.context.annotation.ConfigurationClassParser
     */
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        // 请配置注解参数:ignoreResourceNotFound = true
        try {
            String filename = Objects.requireNonNull(resource.getResource().getFilename());
            //1.创建yaml文件解析工厂
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            //2.设置资源内容
            yaml.setResources(resource.getResource());
            //3.解析成properties文件
            Properties properties = yaml.getObject();
            if (properties == null) {
                throw new FileNotFoundException();
            }
            //4.返回符合spring的PropertySource对象
            return name != null ? new PropertiesPropertySource(name, properties)
                    : new PropertiesPropertySource(filename, properties);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IOException("Yaml解析异常,异常原因:" + e.getMessage());
        }

    }
}

还需要配置一个@PropertySource注解来定义扫描规则:

/**
 * 配置文件管理类:
 * 1. 默认读取位置为 classpath:application.yml。
 * 2. 由 spring管理来自 yml文件读取到的属性,交给 @See {@link YamlPropertySourceFactory} 管理
 */
@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class)
public class PropertySourceConfig {
}

更多Spring扩展请查看专题Spring开发笔记

相关文章

  • PropertySourceFactory的扩展和使用

    扩展点简述 Strategy interface for creating resource-based {@li...

  • 浅谈Kotlin语法篇之扩展函数(五)

    1、为什么要使用Kotlin中的扩展函数? 2、怎么去使用扩展函数和扩展属性? 3、什么是扩展函数和属性? 4、扩...

  • 分类和扩展的使用

    一、类扩展 格式: 说明: 1、类扩展可以为某一个类添加额外的属性,成员变量,方法等,并且都是该类的私有项 2、类...

  • MyBatisPlus的使用和扩展

    1. MyBatisPlus概述 为什么要学习它呢?MyBatisPlus可以节省我们大量工作时间,所有的CRUD...

  • Kotlin-面向对象-进阶

    扩展 扩展方法 Kotlin支持扩展方法和扩展属性。语法:被扩展的类/接口名.方法名() 父类不能使用子类的扩展方...

  • COLA的扩展性使用和源码研究

    cola扩展点使用和设计初探 封装变化,可灵活应对程序的需求变化。 扩展点使用 步骤: 定义扩展点接口,类型可以是...

  • laravel5 使用redis的个人总结

    之前研究使用过yii2,针对redis扩展的使用简单和laravel5比较一下: yii2的扩展使用要比larav...

  • kotlin Reified 理解

    参考 Reified讲解 优缺点 代码理解 使用kotlin的扩展函数和扩展属性为Activity扩展出一个sta...

  • swift分类和扩展的使用

    其实在swift中严格的说是没有分类的一说的,swift建议我们使用扩展功能实现一些系统控件的扩展但是在实际的开发...

  • Kotlin 简化Fragment使用的扩展方法

    为了更方便的使用Frgment,使用扩展方法对其进行扩展,来简化其使用方式 添加扩展函数inline fun Fr...

网友评论

      本文标题:PropertySourceFactory的扩展和使用

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