Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use springBoot to exclude database startup mode

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 215

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report