如何在Spring Boot 2.0中注册我的自定义环境后处理器?

时间:2018-05-23 07:24:03

标签: spring-boot

我按照此documentation中的确切步骤进行操作。

我的META-INF / spring.factories

中有以下条目
org.springframework.boot.env.EnvironmentPostProcessor=com.mygroup.myapp.CustomEnvironmentPostProcessor

我的后处理器:

public class CustomEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {
..
}

我没有在日志中看到任何内容,好像它没有注册或不存在。

我解压缩了JAR,我可以看到META-INF / spring.factories。我也可以直接从root看到BOOT-INF / classes。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:3)

没有优雅的方法来解决这个问题。你可以做这样的事情:

@Component
public class CustomEnvironmentPostProcessor implements
        EnvironmentPostProcessor, ApplicationListener<ApplicationEvent> {

    private static final DeferredLog log = new DeferredLog();

    @Override
    public void postProcessEnvironment(
            ConfigurableEnvironment env, SpringApplication app) {
        log.error("This should be printed");
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        log.replayTo(CustomEnvironmentPostProcessor.class);
    }
}

答案 1 :(得分:0)

定义spring.factories文件

环境后处理器

org.springframework.boot.env.EnvironmentPostProcessor=\
class name with package