Spring Boot 2.0.2 druid用的是1.1.9 集成时 注意filters的 配置

1
升级Spring Boot版本到2.0.2后报下面的错,druid用的是1.1.9
1
2
3
4
5
6
7
8
9
10
11
12
[o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext : 99] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; 
nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$9e89c27b]: Constructor threw exception;

nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'dataSource': Could not bind properties to 'DruidDataSourceWrapper' : prefix=spring.datasource.druid, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.druid' to javax.sql.DataSource

查看堆栈代码,容器没有完成初始化,在查看最后一行堆栈发现 datasource 没有实例成功,跟之间的老版本做了对比发现filters 配置加了log4 就异常。
查看log4Fliter 类。发现移除了log4 包导致无法load成功 。Spring Boot 默认的日志系统是 logback,并没有依赖 log4j,这里配置的 log4j 所以报错了。要想使用 log4j filter 你要把日志系统切换为 log4j。

image.png

1
2
3
正确的配置
druid:
filters: stat,wall