1. Springboot dynamically loads the configuration file on the server
springboot uses the EnvironmentPostProcessor interface to dynamically load configuration files
Specific implementation steps
There is a configuration file in the server directory, as shown below. We need to dynamically load it into springboot and obtain the configuration springboot.name=springboot.
Implement the interface 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();
}
}
}
Read the configuration content in the startup class. Of course, you can also read it from other places. Please refer to the previous blog https://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"));
}
}
Output result:
2. Springboot activates the specified configuration file profile
We know that in actual project development, there are usually multiple sets of configuration files, such as one for development environment, one for test environment, and one for production environment. What if you activate a specified profile?
If there are 3 configuration files now,
One is the default configuration file application.properties, which configures public information, which is the same configuration information in any environment.
ds.name=lsl ds.password=123456
The second is the development environment configuration file application-dev.properties, which configures the database connection for the development environment.
jdbc.url=mysql:/jdbc://127.0.0.1/db_test
The third is the configuration file application-test.properties of the test environment, which configures the database connection of the test environment.
jdbc.url=mysql:/jdbc://127.0.0.1/db_dev
How to specify the application-dev.properties configuration file that activates the development environment now?
The first way: activate through command parameters
--spring.profiles.active=dev
Activate multiple configuration files with command parameters --spring.profiles.active=dev,test
Second way: activate through program
We activate the test configuration file through the program,
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"));
}
}
Output result:
Programmatically activate multiple profiles app.setAdditionalProfiles("test","dev");
There is also a special usage, which is to hand over a certain bean to spring for hosting when a certain profile is activated.
Create a new configuration class and use the @Profile annotation. When dev is activated, creatRable2 is handed over to spring for hosting. When test is activated, creatRable3 is handed over to spring for hosting.
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 ()->{};
}
}
Result output:
By the way, the default configuration file is activated by default.