【问题标题】:logger object in AbstractHealthIndicator is null - Spring BootAbstractHealthIndicator 中的记录器对象为空 - Spring Boot
【发布时间】:2019-06-21 07:14:27
【问题描述】:

当我在数据库关闭时调用heathIndicator.health() 方法时,它会抛出NullPointerException。堆栈跟踪显示 AbstractHealthIndicator 中的 logger 对象为空。我也通过调试验证了它。当数据库启动时,代码可以正常工作,因为没有调用记录器。我不太明白为什么会发生这种情况。

这是Spring的AbstractHealthIndicator

health方法的代码
@Override
    public final Health health() {
        Health.Builder builder = new Health.Builder();
        try {
            doHealthCheck(builder);
        }
        catch (Exception ex) {
            // null pointer exception happens here, logger is null.
            if (this.logger.isWarnEnabled()) {
                String message = this.healthCheckFailedMessage.apply(ex);
                this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
                        ex);
            }
            builder.down(ex);
        }
        return builder.build();
    }

【问题讨论】:

  • 你在扩展AbstractHealthIndicator 吗?你在用spring-boot-2吗?
  • 你是如何初始化记录器变量的?
  • 这不是我的代码,我只是在使用它。是AbstractHealthIndicatorhealth方法的源代码。
  • 哦,好吧,明白了,为什么要担心spring的内部代码?,如果需要实现健康检查,请扩展它并覆盖方法。
  • 我的意思是它正在做我想要的,除了那部分。这甚至不是逻辑问题, logger 出于某种原因只是 null 。顺便说一句,我知道我可以自己实现,但我只是好奇为什么会发生这种情况。

标签: java spring spring-boot spring-boot-actuator health-monitoring


【解决方案1】:

我在AbstractHealthIndicator 类中遇到了同样的问题。在我的情况下,问题是在我的健康指标中使用@Transactional。由于这个注解,Spring 为我的类创建了一个 CGLIB 代理。这应该不是问题,但我猜是因为AbstractHealthIndicator 中的AbstractHealthIndicator.health() 方法是最终的,所以CGLIB 代理无法正常工作。显而易见的解决方案不是使用@Transactional,而是将您的支票委托给服务。

这也是在初始化期间指定的。记录了这样的事情:

 o.s.aop.framework.CglibAopProxy: Unable to proxy interface-implementing method [public final org.springframework.boot.actuate.health.Health org.springframework.boot.actuate.health.AbstractHealthIndicator.health()] because it is marked as final: Consider using interface-based JDK proxies instead!

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 2019-01-13
    • 1970-01-01
    • 2017-08-06
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2020-06-30
    相关资源
    最近更新 更多