第一种: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