EnvironmentPostProcessor是Spring Boot的一个拓展接口,我们可以通过这个接口来添加配置文件。
可以通过http等方式构成一个Properties,实现统一的管理配置文件。
这里只是简单的实现在文件管理下的配置文件。
这是源码的解释,说明了该接口的实现类要在classpath:META-INT/spring.factories里面注册
Allows for customization of the application's {@link Environment} prior to the
application context being refreshed.
<p>
EnvironmentPostProcessor implementations have to be registered in
{@code META-INF/spring.factories}, using the fully qualified name of this class as the
key.
<p>
{@code EnvironmentPostProcessor} processors are encouraged to detect whether Spring's
{@link org.springframework.core.Ordered Ordered} interface has been implemented or if
the @{@link org.springframework.core.annotation.Order Order} annotation is present and
to sort instances accordingly if so prior to invocation.
实现类MyEnvironmentPostProcessor
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;
@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
System.out.println("1");
Properties properties=null;
try{
properties =new Properties();
properties.load(new FileInputStream("E:/SpringBoot/application1.properties"));
PropertiesPropertySource propertySource =new PropertiesPropertySource("ds", properties);
environment.getPropertySources().addLast(propertySource);
}
catch(Exception e){
e.printStackTrace();
}
}
}
spring.factories
org.springframework.boot.env.EnvironmentPostProcessor=com.springboot.www.config.MyEnvironmentPostProcessor