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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章