定义为null的bean'metaDataSourceAdvisor'无法注册

时间:2019-01-06 06:33:54

标签: spring-boot

我已经将Spring Boot版本从2.0.3更新到2.1.1,但是我得到了:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'metaDataSourceAdvisor', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

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

我得到了错误-问题是defined in null并不是查找位置的好指针。我见过this question,但实际上我更希望继续禁止覆盖不明确的bean。

有什么想法吗?


日志输出

2019-01-06 07:33:39.282  INFO 29626 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-01-06 07:33:39.282  INFO 29626 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-01-06 07:33:39.912  INFO 29626 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-01-06 07:33:39.997  INFO 29626 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 80ms. Found 23 repository interfaces.
2019-01-06 07:33:39.999  WARN 29626 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'metaDataSourceAdvisor' defined in null: Cannot register bean definition [Root bean: class [org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'metaDataSourceAdvisor': There is already [Root bean: class [org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
2019-01-06 07:33:40.008  INFO 29626 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-01-06 07:33:40.010 ERROR 29626 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

3 个答案:

答案 0 :(得分:20)

好的,我自己发现了这个问题:我在项目中两次遇到@EnableGlobalMethodSecurity

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // <--
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
}

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"mz.server.spring.repository"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) // <--
@EntityScan(basePackages = "mz.server.hibernate.model")
@EnableTransactionManagement
@EnableScheduling
public class Application {
}

所以我想说这是一个很好的Spring Boot新功能。

如果您看到这种错误,请小心不要重复的注释。

答案 1 :(得分:4)

根据release notes(针对Spring Boot 2.1)扩展接受的答案

  

默认情况下禁用Bean覆盖,以防止意外覆盖Bean。如果您要依赖覆盖,则需要将spring.main.allow-bean-definition-overriding设置为true。

因此,如果您在代码库中多次使用@EnableGlobalMethodSecurity,并且这些bean是同一组件扫描的一部分,则此注释将尝试多次创建metaDataSourceAdvisor bean。这将在初始化期间引发异常。

这还将适用于其他创建bean的自动配置注释。确保仅一次使用它们的启用注释。

答案 2 :(得分:-2)

可以帮助在Gradle或Maven中“清理”和“构建”项目。