Spring boot 中将自定配置文件加入placeHolder

在自定义配置中加入:

package com.cps.trans.route.configuration;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

/**
 * @author max woods
 * @date 2018/8/27
 */

@Configuration
//@ImportResource(locations = {"classpath:config/channel.yml","classpath:config/cups.yml"})
public class CustomConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        // 从classpath导入
        yaml.setResources(new ClassPathResource("/config/channel.yml"),
                new ClassPathResource("/config/cups.yml"));
        yaml.afterPropertiesSet();
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享