学无先后,达者为师

网站首页 编程语言 正文

Springboot Druid 启动报错:Failed to configure a DataSource: ‘url‘ attribute is not specified, 问题解决方案

作者:DangerShi 更新时间: 2022-07-12 编程语言

背景:

该应用(下文中用app代替)之前版本由其他同事已经发布上线过,正常使用中,本次升级了其中依赖的公司某个sdk的版本(下文中用a.jar代替),结果出现应用启动报错,报错信息如下:

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxx':…………(略)

Related cause: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?

…………(略)

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (the profiles qa are currently active).

排查过程:

1、网上最容易查到的解答(来自stackoverflow),在启动类上的注解加如下配置:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }

这种方式针对的是应用中不需要使用数据库的场景,而我们的应用是要使用数据库的,所以并不适用。

2、既然要使用数据库,而报错又是数据库url未配置,那就检查数据库的配置吧。

检查application.properties中的数据库配置是否正确,注意检查格式和内容;经检查,该配置无误(该应用之前正常发布上线,数据库配置本次未做调整);

3、继续检查应用的配置,

项目的Application启动类中有如下注解:

@ImportResource({"classpath*:spring/applicationContext.xml"})

所以检查applicationContext.xml中的配置,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath*:spring/dataSource.xml"/>
    <import resource="classpath*:dubbo/beans-dubbo-consumer.xml"/>

</beans>

检查发现,dataSource.xml的路径配置与实际路径不一致(dataSource.xml中内容的是DruidDataSource配置),并不在spring文件夹下,实际路径为:classpath*:dataSource.xml,修改后该报错消失,应用启动成功。

4、仍然有个疑惑,为什么升级a.jar版本之前,该应用启动正常呢?

排查之前的配置,发现原来在低版本a.jar的pom中引入了b.jar, 而b.jar中是存在spring/dataSource.xml的,应用app与b.jar都是公司同一个业务域下的组件,使用的是同一个数据库,所以歪打正着的使app正常启动(坑啊~)。但由于a.jar中本来是不需要依赖b.jar的,所以a在本次升级的时候,pom中去掉了b.jar的依赖,从而导致了app升级后无法正常启动了。

以上,问题搞定!

原文链接:https://blog.csdn.net/jian876601394/article/details/125149405

栏目分类
最近更新