Spring boot 如何在SpringBoot2.0中注册自定义环境后处理器?

Spring boot 如何在SpringBoot2.0中注册自定义环境后处理器?,spring-boot,Spring Boot,我遵循了这个过程中的确切步骤 我的META-INF/spring.factories中有以下条目 org.springframework.boot.env.EnvironmentPostProcessor=com.mygroup.myapp.CustomEnvironmentPostProcessor 我的后处理器: public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor,

我遵循了这个过程中的确切步骤

我的META-INF/spring.factories中有以下条目

org.springframework.boot.env.EnvironmentPostProcessor=com.mygroup.myapp.CustomEnvironmentPostProcessor
我的后处理器:

public class CustomEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {
..
}
我在日志中没有看到任何东西,好像它没有注册或不存在

我打开罐子的拉链,可以看到META-INF/spring.factories。我还可以直接从根目录中看到BOOT-INF/类


我在这里遗漏了什么?

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

@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);
    }
}
@组件
公共类CustomEnvironmentPostProcessor实现
环境后处理器,应用程序侦听器{
私有静态最终延迟日志=新延迟日志();
@凌驾
公共空间后处理环境(
ConfigurableEnvironment,SpringApplication应用程序){
日志错误(“应打印此日志”);
}
@凌驾
ApplicationEvent上的公共无效(ApplicationEvent事件){
log.replayTo(CustomEnvironmentPostProcessor.class);
}
}

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

@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);
    }
}
@组件
公共类CustomEnvironmentPostProcessor实现
环境后处理器,应用程序侦听器{
私有静态最终延迟日志=新延迟日志();
@凌驾
公共空间后处理环境(
ConfigurableEnvironment,SpringApplication应用程序){
日志错误(“应打印此日志”);
}
@凌驾
ApplicationEvent上的公共无效(ApplicationEvent事件){
log.replayTo(CustomEnvironmentPostProcessor.class);
}
}

定义spring.factories文件

环境后处理器

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

定义spring.factories文件

环境后处理器

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

这个答案是正确的。一些背景:您没有“看到任何东西”,因为日志系统尚未初始化。此时无法初始化它,因为我们使用环境来配置它,而您正在自定义环境。答案是正确的。一些背景:您没有“看到任何东西”,因为日志系统尚未初始化。此时无法初始化它,因为我们使用环境来配置它,而您正在自定义环境。