天天看點

DruidFilterConfiguration.class], could not be registered. A bean with that name has already been....

報錯資訊:

2019-07-23 11:29:09.457  INFO 39488 --- [  restartedMain] com.xxx.xxx.xxxApplication          : Starting xxxApplication on IT-LAPTOP01 with PID 39488 (started by 50015573 in D:\Devops Project\xxx)

2019-07-23 11:29:09.488  INFO 39488 --- [  restartedMain] com.xxx.xxx.xxxApplication          : No active profile set, falling back to default profiles: default

2019-07-23 11:29:09.570  INFO 39488 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable

2019-07-23 11:29:09.570  INFO 39488 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'

2019-07-23 11:29:11.353  WARN 39488 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'statFilter' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration; factoryMethodName=statFilter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.class]] for bean 'statFilter': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=druidConfiguration; factoryMethodName=statFilter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/xxx/xxx/config/DruidConfiguration.class]] bound.

2019-07-23 11:29:11.366  INFO 39488 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2019-07-23 11:29:11.369 ERROR 39488 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************

APPLICATION FAILED TO START

***************************

Description:

The bean 'statFilter', defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/xxx/xxx/config/DruidConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Process finished with exit code 0

解決方案:

從報錯資訊裡面分析提示是could not be registered. A bean with that name has already been defined in class path resource [com/xxx/xxx/config/DruidConfiguration.class] and overriding is disabled,表示DruidConfiguration存在兩個bean加載發生沖突了。

建議把用Druid的DruidFilterConfiguration.class加載排除掉即可,在啟動檔案裡面加上@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,DruidDataSourceAutoConfigure.class}),DruidConfiguration的jar檔案的@Configuration注解保留。不建議在application.properties配置檔案中加spring.main.allow-bean-definition-overriding=true,這樣子可能會引發跟多報錯,有興趣的小夥可以試試。