springboot配置外部yaml配置文件(不同环境加载不同的相应配置)

第一种:jar包运行模式

在启动类中加入方法注入:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;

import java.net.MalformedURLException;

/**
 * @author qiaojun
 */
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication  {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() throws MalformedURLException {
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//		yaml.setResources(new ClassPathResource("D:\\config.yaml"));//获取resources目录下的配置文件
//		yaml.setResources(new FileUrlResource("C:\\Users\\qiaojun\\Desktop\\config.yaml"));//可获取网络资源
		yaml.setResources(new FileSystemResource("C:\\Users\\qiaojun\\Desktop\\config.yaml"));//获取本地路径资源
		configurer.setProperties(yaml.getObject());
		return configurer;
	}

}

第二种:war包在tomcat下运行模式

第一步:新建一个MyEnvironmentPostProcessor类:

package com.demo;

import com.demo.property.StaticProps;
import com.demo.property.SystemProps;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;

/**
 * @author qiaojun
 */
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) {
        //配置不同环境的数据库连接配置
        if(!StaticProps.LOCAL.equals(SystemProps.CURRENT_ENV)){
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new FileSystemResource("/static_resources/test/application.yaml"));
            MutablePropertySources propertySources = environment.getPropertySources();
            propertySources.addFirst(new PropertiesPropertySource("spring.datasource.url", yaml.getObject()));
            propertySources.addFirst(new PropertiesPropertySource("spring.datasource.username", yaml.getObject()));
            propertySources.addFirst(new PropertiesPropertySource("spring.datasource.password", yaml.getObject()));
        }
        /*
            如果同一个参数, application.yml中有定义, 外部配置文件也有定义, 以哪一个为准呢?
            1.以外部配置文件为准:
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
            2.以application.yml中的文件为准:
            propertySources.addLast(new PropertiesPropertySource("Config", properties));
         */
    }
}

第二步:在resources目录下新建一个META-INF目录,然后在该目录下创建一个spring.factories文件

文件内容为:org.springframework.boot.env.EnvironmentPostProcessor=com.demo.MyEnvironmentPostProcessor

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值