日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

欄目分類
最近更新