您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用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})
就可以跳過數據庫的自動初始化,自己為所欲為了~
環境:spring boot 1.5、JDK1.8
# 驅動配置信息 spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456 spring.datasource.driverClassName = com.mysql.jdbc.Driver #連接池的配置信息 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
public DataSource build() { Class<? extends DataSource> type = this.getType(); DataSource result = (DataSource)BeanUtils.instantiate(type); this.maybeGetDriverClassName(); this.bind(result); return result; }
我們可以發現,在沒有配置spring.datasource.type時,spring boot默認的連接池是tomcat-jdbc
也就是說我們在application.properties中配置的連接池參數是無效的。
好,那我們再配置下這個屬性,使用阿里巴巴的druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
再啟動下
再來看看1.5版本org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder的源碼
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] { "org.apache.tomcat.jdbc.pool.DataSource", "com.zaxxer.hikari.HikariDataSource", "org.apache.commons.dbcp.BasicDataSource", // deprecated "org.apache.commons.dbcp2.BasicDataSource" };
spring boot 1.5的版本默認連接池為tomcat-jdbc
spring boot 2.0的版本默認連接池為HikariCP
以上是“如何使用springboot不自動初始化數據庫連接池”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。