接上篇随笔,配置druid之后出现的错误,解决方法如下:
1 @Bean(destroyMethod = "close",initMethod = "init") 2 @ConfigurationProperties(prefix = "spring.datasource") 3 public DataSource druidDataSource() { 4 DruidDataSource datasource = new DruidDataSource(); 5 datasource.setUrl(dbUrl); 6 datasource.setUsername(username); 7 datasource.setPassword(password); 8 datasource.setDriverClassName(driverClassName); 9 datasource.setInitialSize(initialSize); 10 datasource.setMinIdle(minIdle); 11 datasource.setMaxActive(maxActive); 12 datasource.setMaxWait(maxWait); 13 datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); 14 datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); 15 datasource.setValidationQuery(validationQuery); 16 datasource.setTestWhileIdle(testWhileIdle); 17 datasource.setTestOnBorrow(testOnBorrow); 18 datasource.setTestOnReturn(testOnReturn); 19 try { 20 datasource.setFilters(filters); 21 } catch (SQLException e) { 22 logger.error("druid configuration initialization filter", e); 23 } 24 return datasource; 25 }
添加红色字体代码即可,因为本人是使用mybatis,需要配置释放方法以及初始化方法,如果是jpa,就不会出现这个问题,只需要引入以下代码:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-jpa</artifactId> 4 </dependency>