在引入阿里druid的时候,需要配置相关的druid属性,给它用起来
配置信息如下:
yaml配置:
server:
port: 8088
spring:
datasource:
username: root
password: 112358
url: jdbc:mysql://127.0.0.1:3306/testjdbc?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
配置组件注入springboot
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druid() {
return new DruidDataSource();
}
}
但在运行时出现如下bug
APPLICATION FAILED TO START
Description:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Property: spring.datasource.filters
Value: stat,wall,log4j
Origin: class path resource [application.yml]:24:14
Reason: org.apache.log4j.Logger
Action:
Update your application's configuration
经排查发现需要导入log4j,因为yaml中配置了
![](https://img.haomeiwen.com/i18628873/13f70a13819e8e51.png)
在pom文件中配置
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
问题解决。
![](https://img.haomeiwen.com/i18628873/293715207abafbbf.png)
网友评论