第一步:编写MyEnvironmentPostProcessor
package gen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import java.util.HashMap;
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
HashMap<String, Object> map = new HashMap<>();
map.put("logging.level.com.dao", "error");
map.put("server.port", "7400");
map.put("spring.datasource.username", "root");
map.put("spring.datasource.password", "123456");
PropertySource source = new MapPropertySource("myCloudConfig", map);
MutablePropertySources propertySources = environment.getPropertySources();
for (PropertySource propertySource : propertySources) {
if(propertySource.getName().startsWith("applicationConfig: [classpath:/application")){
propertySources.addBefore(propertySource.getName(), source);
break;
}
}
}
}
第二步:编写META-INF/spring.factories
org.springframework.boot.env.EnvironmentPostProcessor=gen.MyEnvironmentPostProcessor