Spring Boot 自定义PropertySourceLoader

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

第一步、

ConfigFileApplicationListener包含了PropertySourcesLoader,这个类会从spring.factories加载PropertySourceLoader的实现类。

PropertySourceLoader就是加载配置接口类。

    public interface PropertySourceLoader {
        String[] getFileExtensions();
        PropertySource<?> load(String name, Resource resource, String profile)
                throws IOException;
    }

getFileExtensions()是返回支持的文件扩展名,比如PropertiesPropertySourceLoader支持的扩展是xml和properties。

ConfigFileApplicationListener定义了默认的文件名DEFAULT_NAMES=“application”,所以SpringBoot会根据文件名加扩展名来加载文件。
load方法会读取配置文件,并返回PropertySource,SpringBoot会从PropertySource读取配置项,合并到总的配置对象中。

何时加载配置文件

所以自定义PropertySourceLoader就需要实现接口类,并配置到spring.factories中。

SpringBoot没有加载json的配置文件,这里就自定义JsonPropertySourceLoader来实现json格式配置文件的加载。

 public class JsonPropertySourceLoader implements PropertySourceLoader {
        public String[] getFileExtensions() {
            return new String[]{"json"};
        }
        public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
            Map<String, Object> result = mapPropertySource(resource);
            return new MapPropertySource(name, result);
        }
        private Map<String, Object> mapPropertySource(Resource resource) throws IOException {
            if (resource == null) {
                return null;
            }
            Map<String, Object> result = new HashMap<String, Object>();
            JsonParser parser = JsonParserFactory.getJsonParser();
            Map<String, Object> map = parser.parseMap(readFile(resource));
            nestMap("", result, map);
            return result;
        }
        private String readFile(Resource resource) throws IOException {
            InputStream inputStream = resource.getInputStream();
            List<Byte> byteList = new LinkedList<Byte>();
            byte[] readByte = new byte[1024];
            int length;
            while ((length = inputStream.read(readByte)) > 0) {
                for (int i = 0; i < length; i++) {
                    byteList.add(readByte[i]);
                }
            }
            byte[] allBytes = new byte[byteList.size()];
            int index = 0;
            for (Byte soloByte : byteList) {
                allBytes[index] = soloByte;
                index += 1;
            }
            return new String(allBytes);
        }
        private void nestMap(String prefix, Map<String, Object> result, Map<String, Object> map) {
            if (prefix.length() > 0) {
                prefix += ".";
            }
            for (Map.Entry entrySet : map.entrySet()) {
                if (entrySet.getValue() instanceof Map) {
                    nestMap(prefix + entrySet.getKey(), result, (Map<String, Object>) entrySet.getValue());
                } else {
                    result.put(prefix + entrySet.getKey().toString(), entrySet.getValue());
                }
            }
        }

总结:

配置PropertySourceLoader,在工程下新建/META-INF/spring.factories文件,并配置JsonPropertySourceLoader。
org.springframework.boot.env.PropertySourceLoader=org.wcong.test.springboot.JsonPropertySourceLoader

main入口,取出customize.property.message并打印出来。

    @Configuration
    @EnableAutoConfiguration
    public class CustomizePropertySourceLoader {
        @Value("${customize.property.message}")
        private String message;
        public static void main(String[] args) {
            SpringApplication springApplication = new SpringApplication(CustomizePropertySourceLoader.class);
            springApplication.setWebEnvironment(false);
            ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
            CustomizePropertySourceLoader customizePropertySourceLoader = configurableApplicationContext.getBean(CustomizePropertySourceLoader.class);
            System.out.println(customizePropertySourceLoader.message);
        }
    }

相关工具如下:

分析:

小结:

参考资料和推荐阅读

1.链接: (https://blog.csdn.net/catoop/article/details/71157986).
2.链接(https://blog.csdn.net/jw0104/article/details/89899817)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值