Springboot扩展:EnvironmentPostProcessor


1.简介

EnvironmentPostProcessor可以在创建应用程序上下文之前,添加或者修改环境配置。

EnvironmentPostProcessor接口实现代表:ConfigFileApplicationListener

2.使用

@Order(Ordered.LOWEST_PRECEDENCE)
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private final Properties properties = new Properties();
    private static final String SERVER_PORT = "server.port";
    private String[] profiles = {
            "db.properties",
    };

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        System.out.println("---CustomEnvironmentPostProcessor");
        // 获取当前应用中的配置信息
        String property = environment.getProperty(SERVER_PORT);
        System.out.println(property);
        // 自定义修改后添加进去覆盖
        HashMap<String, Object> map = new HashMap<>(1);
        map.put(SERVER_PORT, "8099");
        PropertySource source = new MapPropertySource("xixi", map);
        environment.getPropertySources().addFirst(source);

        //添加自定义环境信息
        for (String profile : profiles) {
            Resource resource = new ClassPathResource(profile);
            try {
                properties.load(resource.getInputStream());
                environment.getPropertySources().addLast(new PropertiesPropertySource(resource.getFilename(), properties));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(environment.getProperty("db.name"));
        System.out.println(environment.getProperty("db.user"));
    }
}

最后一定要在META-INF/spring.factories中添加使之生效。

org.springframework.boot.env.EnvironmentPostProcessor=com.yue.test.CustomEnvironmentPostProcessor

3.触发点

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        // EnvironmentPostProcessor触发点
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
        context = createApplicationContext();
        ...
    }
    ...
    return context;
}

//环境准备
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
        ApplicationArguments applicationArguments) {
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    ConfigurationPropertySources.attach(environment);
    // 发布环境准备完成广播事件
    listeners.environmentPrepared(environment);
    bindToSpringApplication(environment);
    if (!this.isCustomEnvironment) {
        environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                deduceEnvironmentClass());
    }
    ConfigurationPropertySources.attach(environment);
    return environment;
}

最终是在 ConfigFileApplicationListener 中调用自定义EnvironmentPostProcessor

 

private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
    List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
    postProcessors.add(this);
    AnnotationAwareOrderComparator.sort(postProcessors);
    for (EnvironmentPostProcessor postProcessor : postProcessors) {
        postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM