springboot加载自定义配置文件(含单元测试)

简单记录问题排查中学习到的知识,欢迎大神修正、补充与完善

加载自定义配置文件4种方式

  1. 第一种:

    继承org.springframework.context.support.PropertySourcesPlaceholderConfigurer类,主要重写mergeProperties()方法,然后注入成bean.

	/** 配置解密,只能自定义一个PropertySourcesPlaceholderConfigurer,否则会报异常 **/
	public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean {

	    /** 需要解密的配置项前缀 */
	    private static final String PREFIX_ENC = "enc:";

	    @Override
	    protected Properties mergeProperties() throws IOException {
	        Properties mergedProperties = new Properties();
	        for (Properties localProp : localProperties) {
	            mergedProperties.putAll(localProp);
	        }

	        for (Map.Entry entry : mergedProperties.entrySet()) {
	            if (entry.getValue().toString().startsWith(PREFIX_ENC)) {
	                String key = System.getProperty("enc.key");
	                String value = entry.getValue().toString().replace(PREFIX_ENC, StringUtils.EMPTY);
	                mergedProperties.setProperty(entry.getKey().toString(), AESUtil.decode(value, key));
	            }
	        }

	        //针对sharding-jdbc datasource自定义解密的特殊处理
	        //因为sharding-jdbc的datasource注入是从environment中获取propertySource,
	        //不能直接通过PropertySourcesPlaceholderConfigurer定义的resource获取
	        MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
	        sources.addFirst(new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergedProperties));

	        return mergedProperties;
	    }

	    @Override
	    public void afterPropertiesSet() throws Exception {
	        localOverride = true;
	    }
	}

	@Bean
    public PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        PropertySourcesPlaceholderConfigurer config = new EncryptPropertyPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("application-dev.yml"));
        config.setProperties(yaml.getObject());
        return config;
    }
  1. 第二种:

    application.yml文件引用方式,被引用文件的文件名格式"application-*.yml",文件放置范围与springboot默认配置文件(4个路径)路径相同

	spring:
      profiles:
        include: aliConfig
  1. 第三种:

    注解方式,如果为yml文件,需要自定义解析类

	@PropertySource(value= {"classpath:aliConfig$a.yml"},factory=InitAliConfig.class)
	public class InitAliConfig implements PropertySourceFactory{
		@Override
		public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
			// 重写这个方法
		}
	}
  1. 第四种:

    启动类properties属性设置:

	SpringApplication sa = new SpringApplication(ZyglScheduleJobApplication.class);
	sa.setDefaultProperties(new InitAliConfig().initAliConfig());
	sa.run(args);

单元测试加载方式

完全支持第一种和第二种
第三种只支持properties文件,不支持yml文件
不支持第四种方式,无设置入口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值