@PropertySource 读取 自定义 yml 不生效问题

# 地址 + 字段 映射,可配多个
proxy-api:
  url-mapping:
    {
      # 访问 /proxy/api/demo/list 就会打开 http://127.0.0.1:9090/api/demo/list
      "[/proxy/api/demo/list]": "http://127.0.0.1:9090/api/demo/list"
    }
  field-mapping:
    {
      "[/proxy/api/demo/list]": {
        "success": "0000",
        "code": "responseCode",
        "msg": "message",
        "data": "data"
      }
    }
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Map;

/**
 * 代理 地址 + 字段 映射配置
 *
 * @author jason
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "proxy-api")
@PropertySource(value = {"classpath:proxy-mapping.yml"}, factory = YamlConfigFactory.class)
public class ProxyMappingConfig {

    /**
     * url 映射
     */
    private Map<String, String> urlMapping;

    /**
     * 字段 映射
     */
    private Map<String, Map<String, String>> fieldMapping;

}
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.util.Properties;

/**
 * PropertySource读取 自定义yml 不生效问题
 *
 * @author jason
 */
public class YamlConfigFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
        String sourceName = resource.getResource().getFilename();
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        factory.afterPropertiesSet();
        Properties propertiesFromYaml = factory.getObject();
        assert sourceName != null;
        assert propertiesFromYaml != null;
        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值