springboot不自动初始化数据库连接池

简介

有时候我们想自己动态的初始化数据库连接池,但是springboot 的@SpringBootApplication注解会自动去初始化数据库连接池,不配置的话会启动失败,如下提示

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$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
INFO  - Unregistering JMX-exposed beans on shutdown

解决方案

办法就是排除自动初始化的类


@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application implements CommandLineRunner {
...
}

加上这么一句 (exclude = {DataSourceAutoConfiguration.class}),就可以跳过数据库的自动初始化,自己为所欲为了

引用

https://www.cnblogs.com/zhaojinxin/p/7641978.html
https://www.cntofu.com/book/79/2-Spring4-快速入门.md

    原文作者:天已青色等烟雨来
    原文地址: https://blog.csdn.net/x356982611/article/details/95355406
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞