springBoot 排除數據庫啟動
1. 場景
在家里運行項目,運行springBoot的時候報數據庫連接不了,公司的數據庫家里不能連接。
2. 配置
2.1 保留之前的properties 配置,不刪除;
2.2 在啟動類中添加
exclude = {DataSourceAutoConfiguration.class}
package cn.cncommdata.file; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 啟動類 */ @SpringBootApplication(scanBasePackages = "cn.cncommdata", exclude = {DataSourceAutoConfiguration.class}) @EnableFeignClients(basePackages = {"cn.cncommdata", "cc.iooc"}) @MapperScan("cn.cncommdata.file.dao") public class FileApplication { /** * main * * @param args args */ public static void main(String[] args) { SpringApplication.run(FileApplication.class, args); } /** * 沒什么用,就是不想讓checkstyle報錯 */ public void init() { } }
Springboot不經過數據庫直接啟動
問題
之前開發工程中,有一個單獨注冊在nacos上服務的項目(不需要訪問數據庫)需要啟動,但是啟動會報錯。
錯誤
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘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.DataSourcePropertiesDataSourceBeanCreationException: Failed to determine a suitable driver class
原因
SpringBoot項目默認會訪問數據庫,因為此時沒有寫數據庫連接,所以出現這個錯誤。
解決
在啟動類上加上一個注解,在容器加載的時候默認排除數據庫連接即可。
具體如圖所示:
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/leinminna/article/details/102932498