Caused by: java.lang.NullPointerException
    at org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics.initialize(DataSourcePublicMetrics.java:64) ~[spring-boot-actuator-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]


我也禁用了指标,但没有运气

endpoints.enabled=false
endpoints.autoconfig.enabled=false
endpoints.metrics.enabled=false


即使禁用度量标准,也总是创建DataSourcePublicMetrics bean。当数据库连接不可用时,这将导致NullPointerException,从而导致Spring Boot无法启动。

最佳答案

我正在使用Hikari数据源,当数据库不可用时,它无法构造数据源对象。因此,来自DataSourcePublicMetrics bean的NPE。我可以解决创建用数据库配置延迟初始化的Hikari数据源的问题,即使数据库不可用时也可以在数据库可用时稍后使用。不知道为什么HikariDataSource没有惰性初始化的构造函数。它确实具有默认构造函数,但不能使用任何setter方法来设置数据库配置。这对于不需要始终启动数据库的应用程序很有用。

public class LazyConnectionDataSource extends HikariDataSource {
    public LazyConnectionDataSource(HikariConfig config) {
        config.validate();
        config.copyState(this);
    }
}

09-30 23:09
查看更多