SpringBoot版本:2.1.1
接上一篇博文。
编写EnvironmentPostProcessor接口实现,重写postProcessEnvironment方法。
@Component
public class MyEnvironmentPropertySource implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
try {
FileInputStream fileInputStream = new FileInputStream(new File("E:\\AllWorkSpace\\custom.properties"));
Properties properties = new Properties();
properties.load(fileInputStream);
//第一个参数自定义
PropertiesPropertySource propertySource = new PropertiesPropertySource("my", properties);
environment.getPropertySources().addLast(propertySource);
} catch (Exception e) {
e.printStackTrace();
}
}
}
是不是以为这样就可以了呢,还差一步,可以到EnvironmentPostProcessor接口里面去看看。