一、springboot动态加载服务器上的配置文件
springboot利用EnvironmentPostProcessor接口来动态加载配置文件
具体实现步骤
服务器目录下有个配置文件,如下图,我们要实现动态加载到springboot中,并取得里面的配置springboot.name=springboot
实现接口EnvironmentPostProcessor
package com.lsl.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
try{
InputStream input = new FileInputStream("D:/temp/springboot.properties");
Properties source = new Properties();
source.load(input);
PropertiesPropertySource propertySource = new PropertiesPropertySource("my",source);
environment.getPropertySources().addLast(propertySource);
}catch (IOException e){
e.printStackTrace();
}
}
}
在启动类里读取该配置内容,当然也可以在其他地方读取,参考上篇bloghttps://blog.csdn.net/dhklsl/article/details/114985492
package com.lsl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootProviderApplication {
public static void main(String[] args) {
ConfigurableApplicationContext content = SpringApplication.run(SpringbootProviderApplication.class, args);
System.out.println("springname=" + content.getEnvironment().getProperty("springboot.name"));
}
}
输出结果:
二、springboot激活指定的配置文件profile
我们知道,在实际的项目开发中,一般会有多套配置文件,比如开发环境一套,测试环境一套,生产环境一套。如果激活指定的profile呢?
假如现在有3个配置文件,
一个是默认的配置文件application.properties,里面配置的是公共信息,也就是在任何环境都一样的配置信息
ds.name=lsl ds.password=123456
第二个是开发环境配置文件application-dev.properties,里面配置的是开发环境的数据库连接
jdbc.url=mysql:/jdbc://127.0.0.1/db_test
第三个是测试环境的配置文件application-test.properties,配置的是测试环境的数据库连接
jdbc.url=mysql:/jdbc://127.0.0.1/db_dev
现在怎么指定激活开发环境的application-dev.properties配置文件呢?
第一种方式:通过命令参数激活
--spring.profiles.active=dev
命令参数方式激活多个配置文件 --spring.profiles.active=dev,test
第二种方式:通过程序激活
我们通过程序激活test配置文件,
package com.lsl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootProviderApplication {
public static void main(String[] args) {
// ConfigurableApplicationContext content = SpringApplication.run(SpringbootProviderApplication.class, args);
SpringApplication app = new SpringApplication(SpringbootProviderApplication.class);
app.setAdditionalProfiles("test");
ConfigurableApplicationContext content = app.run(args);
System.out.println("springname=" + content.getEnvironment().getProperty("springboot.name"));
System.out.println("ds.name=" + content.getEnvironment().getProperty("ds.name"));
System.out.println("ds.password=" + content.getEnvironment().getProperty("ds.password"));
System.out.println("jdbc.url=" + content.getEnvironment().getProperty("jdbc.url"));
}
}
输出结果:
编程的方式激活多个配置文件app.setAdditionalProfiles("test","dev");
还有一种特殊用法,是当某个profile激活时把某个bean交给spring托管。
新建一个配置类,利用@Profile注解,当dev激活时,把creatRable2交给spring托管,当test激活时把creatRable3交给spring托管
package com.lsl;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
@SpringBootConfiguration
public class MyConfig {
@Bean
public Runnable createRable1(){
System.out.println("=======default======");
return ()->{};
}
@Bean
@Profile("dev")
public Runnable createRable2(){
System.out.println("=======dev======");
return ()->{};
}
@Bean
@Profile("test")
public Runnable createRable3(){
System.out.println("=======test======");
return ()->{};
}
}
结果输出:
对了,默认的配置文件是默认激活状态的。