Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use springBoot to exclude database startup mode

Shulou Source: shulou.com Published: 2022-06-03 16:13:03 11月14日 Update

This article shows you how to use springBoot to exclude database startup mode, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

SpringBoot excludes database startup 1. Scene

When running the project at home, it is reported that the database cannot be connected when running springBoot, and the company's database cannot be connected at home.

two。 Configuration

2.1 keep the previous properties configuration and not delete it

2.2 add to the startup class

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 / * launch class * / @ 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) } / * is useless, just don't want checkstyle to report an error * / public void init () {}} Springboot starts directly without going through the database

In the previous development project, a separate project registered with the nacos service (which does not require database access) needed to be started, but the startup reported an error.

Error

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

Reason

The SpringBoot project accesses the database by default because no database connection is written at this time, so this error occurs.

Solve

Add a note to the startup class to exclude database connections by default when the container is loaded.

As shown in the figure:

The above content is how to use springBoot to exclude database startup mode, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Data database project method content that is skills time knowledge errors running configuration concise nothing concise company reason scenario container engineering Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Huawei Linux vpn