控制台出现Failed to bind properties under ‘spring.datasource‘ to javax.sql.DataSource

朴灿烈づ我的快乐病毒、 2023-10-04 12:01 201阅读 0赞

网上查了下,没有找打相关的报错解决办法,所以在解决问题后,整理到网上,帮助有需要的朋友。

springboot整合druid时,引入了druid的数据源,在配置文件application.yml中配置了相关配置

  1. initialSize: 5
  2. minIdle: 5
  3. maxActive: 20
  4. maxWait: 60000
  5. timeBetweenEvictionRunsMillis: 60000
  6. minEvictableIdleTimeMillis: 300000
  7. validationQuery: SELECT 1 FROM DUAL
  8. testWhileIdle: true
  9. testOnBorrow: false
  10. testOnReturn: false
  11. poolPreparedStatements: true
  12. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  13. filters: stat,wall,log4j
  14. maxPoolPreparedStatementPerConnectionSize: 20
  15. useGlobalDataSourceStat: true
  16. connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

也作了相关配置 DruidConfig.class

  1. @Configuration
  2. public class DruidConfig {
  3. @ConfigurationProperties(prefix = "spring.datasource")
  4. @Bean
  5. public DataSource druid() {
  6. return new DruidDataSource();
  7. }
  8. // 配置Druid的监控
  9. // 1、配置一个管理后台的Servlet
  10. @Bean
  11. public ServletRegistrationBean statViewServlet() {
  12. ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  13. Map<String, String> initParams = new HashMap<>();
  14. initParams.put("loginUsername", "admin");
  15. initParams.put("loginPassword", "123456");
  16. initParams.put("allow", "");// 默认就是允许所有访问
  17. initParams.put("deny", "10.18.172.124");
  18. bean.setInitParameters(initParams);
  19. return bean;
  20. }
  21. // 2、配置一个web监控的filter
  22. @Bean
  23. public FilterRegistrationBean webStatFilter() {
  24. FilterRegistrationBean bean = new FilterRegistrationBean();
  25. bean.setFilter(new WebStatFilter());
  26. Map<String, String> initParams = new HashMap<>();
  27. initParams.put("exclusions", "*.js,*.css,/druid/*");
  28. bean.setInitParameters(initParams);
  29. bean.setUrlPatterns(Arrays.asList("/*"));
  30. return bean;
  31. }
  32. }

但是在启动时报错:


APPLICATION FAILED TO START


Description:

控制台出现Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource:

  1. Property: spring.datasource.filters
  2. Value: stat,wall,log4j
  3. Origin: class path resource [application.yml]:24:14
  4. Reason: org.apache.log4j.Logger

Action:

Update your application’s configuration

根据报错提示在配置文件的24行,查看配置文件,该行代码是 filters: stat,wall,log4j

看报错原因Reason: org.apache.log4j.Logger,于是猜想少了log4j的相关依赖,在pom中引入相关依赖

  1. <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  2. <dependency>
  3. <groupId>log4j</groupId>
  4. <artifactId>log4j</artifactId>
  5. <version>1.2.17</version>
  6. </dependency>

再次启动,成功!

发表评论

表情:
评论列表 (有 0 条评论,201人围观)

还没有评论,来说两句吧...

相关阅读