解决报错 Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException Annotation

Springboot 项目启动报错  

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'shiroConfig' for bean class [com.xxx.config.ShiroConfig] conflicts with existing, non-compatible bean definition of same name and class [org.jeecg.config.ShiroConfig] 

 

原因:  因为我的项目是基于开源项目jeecg开发的, 在jeecg中已经有了ShiroConfig这个bean,  而在我本地的项目中也同样存在  com.xxx.config.ShiroConfig 这个bean, 

然后项目启动的时候我会去扫描 com.xxx.* 和 org.jeecg.*,  所以就会报错bean冲突的错误

 

 

解决方法: 在Springboot的启动类上, 把org.jeecg.config.ShiroConfig这个bean给排除掉, 也就是当项目启动的时候不去扫描它, 不让它加载

代码实现:

 

@ComponentScan(basePackages={"org.jeecg","com.xxx"},
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = {"org.jeecg.config.ShiroConfig"}))

 

 

 

1.@ComponentScan注解作用

@ComponentScan用于类或接口上主要是指定扫描路径,spring会把指定路径下带有指定注解的类自动装配到bean容器里。会被自动装配的注解包括@Controller、@Service、@Component、@Repository等等。其作用等同于<context:component-scan base-package="com.maple.learn" />配置

2.@ComponentScan使用

常用属性如下:

  • basePackages、value:指定扫描路径,如果为空则以@ComponentScan注解的类所在的包为基本的扫描路径
  • basePackageClasses:指定具体扫描的类
  • includeFilters:指定满足Filter条件的类
  • excludeFilters:指定排除Filter条件的类

includeFilters和excludeFilters 的FilterType可选:ANNOTATION=注解类型 默认、ASSIGNABLE_TYPE(指定固定类)、ASPECTJ(ASPECTJ类型)、REGEX(正则表达式)、CUSTOM(自定义类型),自定义的Filter需要实现TypeFilter接口

 

详细用法可以参考其他文章

 

 

 

 

还有另外一种情况就是, 在项目中定义了两个相同名称的bean,  解决方式就是在对应的类的  @Controller、@Service、@Component、@Repository 后指定该bean的名称与另外一个同名称的bean区分开了即可   

例如:@Service("test")   @Service("test2")

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值