开发项目时,整合spring-security时出现了这个错误
*************************** APPLICATION FAILED TO START *************************** Description: The bean 'metaDataSourceAdvisor' could not be registered. A bean with that name has already been defined and overriding is disabled. Action: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true Disconnected from the target VM, address: '127.0.0.1:59676', transport: 'socket' Process finished with exit code 0
很明显是Bean定义重复,但是在这个项目里我并没有定义这个Bean
也就是spring-security定义的
全局搜索报错信息
报错信息是拼接一起的,分开来搜索
找到下面

还有

很明显在这,也就是MethodSecurityMetadataSourceAdvisorRegistrar
在这个类上,点击ctrl+鼠标左键
定位到GlobalMethodSecurity

全局搜索GlobalMethodSecurity,可以看到有一个注解

就是这里了,还好能定位到这里
就是这个注解重复定义的问题了
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
一般是在你的模块里重复注解了几次
我更复杂,在另一个依赖的模块里重复定义,还好能找到
这里提一句
还有一种做法是
spring:
main:
allow-bean-definition-overriding: true这是允许在 Spring 应用上下文中有多个相同名称的 Bean。如果你有多个相同名称的 Bean 定义,Spring 会允许第二个定义覆盖第一个,而不会抛出错误。
注意,很明显当然不能这样


