Spring引导静态内部类在应用程序作为jar启动时未初始化

use*_*397 6 spring initialization mdc spring-boot spring-cloud-sleuth

当我通过intellij启动我的spring启动应用程序时,下面的类正确初始化并且可以看到日志.但如果我从构建中再次运行jar,MyCurrentTraceContext似乎没有初始化,我也看不到输出中的日志.我确实需要这个类和我的定制逻辑来运行一些参数到MDC.有什么建议?

@Configuration
@ConditionalOnProperty(value="spring.sleuth.enabled", matchIfMissing=true)
@AutoConfigureBefore(TraceAutoConfiguration.class)
public class MyLogConfiguration extends SleuthLogAutoConfiguration {
    private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(WtrLogConfiguration.class);

    @Configuration
    @ConditionalOnClass(MDC.class)
    @EnableConfigurationProperties(SleuthSlf4jProperties.class)
    protected static class MySlf4jConfiguration extends Slf4jConfiguration {

        @Bean
        @ConditionalOnProperty(value = "spring.sleuth.log.slf4j.enabled", matchIfMissing = true)
        @ConditionalOnMissingBean
        @Override
        public CurrentTraceContext slf4jSpanLogger() {
            LOGGER.info("************ OVER WRITTING WTIH WtrCurrentTraceContext*******");
            return new MyCurrentTraceContext(Slf4jCurrentTraceContext.create());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pas*_*ibi 0

请检查

@ComponentScan

在入门类的顶部设置项目启动时需要扫描的路径。在你的情况下,那就是:

@ComponentScan("myPackagePath.MyLogConfiguration.MySlf4jConfiguration.*")