【Spring Boot】Error creating bean with name ‘servletEndpointRegistrar‘ defined in class

末蓝、 2024-04-18 17:25 148阅读 0赞
  1. org.springframework.context.ApplicationContextException: Unable to start
  2. web server; nested exception is java.lang.RuntimeException:
  3. org.springframework.beans.factory.BeanCreationException: Error creating
  4. bean with name 'servletEndpointRegistrar' defined in class path resource
  5. [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed;
  6. nested exception is
  7. org.springframework.beans.BeanInstantiationException: Failed to
  8. instantiate
  9. [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested
  10. exception is org.springframework.beans.factory.BeanCreationException:
  11. Error creating bean with name 'healthEndpoint' defined in class path
  12. resource
  13. [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested
  14. exception is org.springframework.beans.BeanInstantiationException:
  15. Failed to instantiate
  16. [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method
  17. 'healthEndpoint' threw exception; nested exception is
  18. org.springframework.beans.factory.BeanCreationException: Error creating
  19. bean with name
  20. 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed;
  21. nested exception is
  22. org.springframework.beans.BeanInstantiationException: Failed to
  23. instantiate
  24. [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfigurationEnhancerBySpringCGLIB96597db7]: Constructor
  25. threw exception; nested exception is
  26. org.springframework.beans.factory.BeanCreationException: Error creating
  27. bean with name 'dataSource': Post-processing of FactoryBean's singleton
  28. object failed; nested exception is
  29. org.springframework.beans.factory.BeanCreationException: Error creating
  30. bean with name 'scopedTarget.dataSource' defined in class path resource
  31. [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested
  32. exception is org.springframework.beans.BeanInstantiationException:
  33. Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory
  34. method 'dataSource' threw exception; nested exception is
  35. org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

解决:

上述的报错其实是JDBC数据库连接池在加载多数据源时没有读取到配置信息引起的。

为了描述清楚这个问题,我假设现在有一个Maven项目A,B、C、D分别是A的子模块(Maven Module),B作为A的基础模块,无论C、D模块是否存在只要A启动必须依赖B。

正常的情况下,B打包时带主类,而C和D在Maven打包的时候是不带主类作为组件包进行打包的,这样的话B和C、D模块就能够实现解耦。

但这次的错误在于JDBC连接池代码是在B模块中,而B+D时,D的Config Server配置中没有包含数据库连接池配置(Gitlab仓库配置中没有数据源配置信息)。因此在checkout下来的文件中并不存在连接池信息。而B需要启动时加载JDBC连接池,所以因为读取不到配置信息而报错。

面对这种情况要不就改变项目架构,要不就是在D的配置文件中加入连接池信息(如果是这个方案建议是在本地项目的bootstrap.yml中配置,因为GitLab中的是公共配置如果没有必要的就不要动了,宁愿在本地根据环境进行配置文件区分写死了算了)。当然了,最好肯定是稍微重构一下项目架构了。

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读