@PropertySource注解解析自定义配置文件

@PropertySource 解析自定义配置文件

使用 @PropertySource 注解,可以编写配置文件的映射类,取代 @Value,在需要用到的地方,用 @Autowire 自动装配配置类即可。

  • 源码:

    public @interface PropertySource {
        
        /** 指示此属性源的名称。如果省略, factory 将基于基础资源生成一个名称(默认是 DefaultPropertySourceFactory) */
        String name () default "";
    
        /** 
         * 指示要加载的属性文件的资源位置。 支持传统属性文件格式和基于 XML 的属性文件格式,例如 "classpath:/com/myco/app.properties" 或 "file:/path/to/file.xml"
         * 不允许使用资源位置通配符(例如 **\/*.properties);每个位置的计算结果必须恰好为一个 .properties 或 .xml 资源。
         */
        String [] value ();
        
        /** 此属性为根据资源路径找不到文件后是否报错,默认为是 false */
        boolean ignoreResourceNotFound () default false;
    
        /** 此为读取文件的编码,若配置中有中文建议使用 'utf-8' */
        String encoding () default "";
        
        /**
         *  指定解析配置文件的工厂类 默认为:'PropertySourceFactory.class',且 PropertySourceFactory 默认只有一个实现  DefaultPropertySourceFactory
         */
        Class<? extends PropertySourceFactory> factory () default PropertySourceFactory.class;
    }
    

PropertySourceFactory 接口

  • 源码:

    public interface PropertySourceFactory {
    	/**
      	 *创建一个包装给定资源的。PropertySource
      	 */
      	PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException;
    
      }
    

DefaultPropertySourceFactory

  • 源码:

      public class DefaultPropertySourceFactory implements PropertySourceFactory {
    	@Override
    	public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
    		return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
    	}
    }
    

解析yml配置文件

  • 使用 @PropertySource 默认可以指定解析 properties 和 xml 配置文件,但不可以解析 yaml 配置文件,因为 spring 默认只有一个解析 properties 和 xml 文件的工厂类 DefaultPropertySourceFactory,没有解析 yaml 的工厂类。
创建解析yml文件的工厂类
  • 引入依赖:

    spring 解析 yaml 依赖。(博主在springblade框架中测试,加不加这个依赖没有影响)

    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.33</version>
    </dependency>
    
  • 创建YamlPropertySourceFactory:

    public class YamlPropertySourceFactory implements PropertySourceFactory {
        @Override
        public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            // 将“.yml”(或“.yaml”)文件加载到 PropertySource
         return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()).get(0);
    
        }
    }
    
  • 配置类添加注解:

    @Data
    @Component
    @PropertySource(value = "classpath:/config/wxplay.yml", factory = YamlPropertySourceFactory.class)//指定加载配置文件的工厂类
    //@PropertySource(value = "classpath:/config/wxplay.properties")//properties类型的配置文件不需要指定加载工厂类
    @ConfigurationProperties(prefix = "wxpay")
    public class WxPlayConfig {
    
        /**
         * 微信小程序appid
         */
        private String appId;
    
        /**
         * 微信小程序secretId
         */
        private String secret;
    
        /**
         * 商户号
         */
        private String mchid;
    
        /**
         * 商户密钥
         */
        private String mchKey;
    
        /**
         * 回调地址
         */
        private String notifyUrl;
    
        /**
         * 证书地址
         */
        private String certPath;
    
    }
    
  • yml配置文件:

    wxpay:
      appId: 123456
      secret: 1234567890
      mchid: 1000001
      mchKey: qerqeetetyeyutrurtutyityityit
      notifyUrl: https://www.baidu.com
      certPath: /apiclient_cert.p12
    
  • 接口测试:

        @Resource
        private WxPlayConfig wxPlayConfig;
    
        @GetMapping("/getPlayConfig")
        public R getWxPlayConfig() {
            return R.data(wxPlayConfig);
        }
    
  • 结果:

    {
    	"code": 200,
    	"success": true,
    	"data": {
    		"appId": "123456",
    		"secret": "1234567890",
    		"mchid": "1000001",
    		"mchKey": "qerqeetetyeyutrurtutyityityit",
    		"notifyUrl": "https://www.baidu.com",
    		"certPath": "/apiclient_cert.p12"
    	},
    	"msg": "操作成功"
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值