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

由于项目升级,遇到了这个问题,找了好多解决办法。都无济于事。

有说是少了引用jdbc包的;有说是需要把DataSource在启动类上排除掉的,都不能解决。

最后找到了这个文章,由于项目的架构不是本人做的,里面的引用超级复杂,结构也很复杂。最后架构的人自行解决了,不过我想应该就是因为下面说的原因导致的。

转载作者的文章以此记录。嘎嘎嘎。

org.springframework.context.ApplicationContextException: Unable to start
web server; nested exception is java.lang.RuntimeException:
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'servletEndpointRegistrar' defined in class path resource
[org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'healthEndpoint' defined in class path
resource
[org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested
exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate
[org.springframework.boot.actuate.health.HealthEndpoint]: Factory method
'healthEndpoint' threw exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name
'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfigurationEnhancerBySpringCGLIB96597db7]: Constructor
threw exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'dataSource': Post-processing of FactoryBean's singleton
object failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'scopedTarget.dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested
exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory
method 'dataSource' threw exception; nested exception is
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中的是公共配置如果没有必要的就不要动了,宁愿在本地根据环境进行配置文件区分写死了算了)。当然了,最好肯定是稍微重构一下项目架构了。

转自:https://blog.csdn.net/kida_yuan/article/details/100691001

猜你喜欢

转载自blog.csdn.net/suixinsuoyu12519/article/details/112387811