啟動項目時,控制臺輸出了如下錯誤信息:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-04-14 21:13:33.005 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter [] - ***************************
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 (no profiles are currently active).
原因:
Spring Boot 在啟動時會自動嘗試注入數據源并配置 JPA(Java Persistence API)。然而,在當前項目的配置環境下,并沒有提供明確的數據源url
屬性,也無法自動配置嵌入式數據源,同時還無法確定合適的驅動類。這就導致了數據源配置失敗,及導致了啟動失敗
解決
我們需要在@SpringBootApplication
注解中排除數據源和 JPA 的自動注入。通過這種方式,Spring Boot 在啟動時就不會再嘗試進行默認的數據源和 JPA 配置,從而避免因配置缺失而導致的啟動失敗。經過這樣的調整后,項目成功啟動。
增加:exclude={DataSourceAutoConfiguration.class}
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
驗證
通過訪問http://127.0.0.1:8082/? ?驗證成功