首页 > 其它 > Spring boot 中将自定配置文件加入placeHolder
2018
10-11

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;
    }
}
最后编辑:
作者:第五维
这个作者貌似有点懒,什么都没有留下。