Spring Boot 02 EnvironmentPostProcessor接口

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值