加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 具有相同类名(不同包)的两个Spring Service Beans即使使

发布时间:2020-12-15 08:26:03 所属栏目:Java 来源:网络整理
导读:我有以下…… package package1;@Service@Qualifier("kb")public class UserService {...}package package2;@Service@Qualifier("user")public class UserService {...}@Autowired@Qualifier("user")package2.UserService p2;@Autowired@Qualifier("kb")pack
我有以下……

package package1;

@Service
@Qualifier("kb")
public class UserService {
...
}

package package2;

@Service
@Qualifier("user")
public class UserService {
...
}

@Autowired
@Qualifier("user")
package2.UserService p2;
@Autowired
@Qualifier("kb")
package1.UserService p1;

但当我尝试运行时,我得到……

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [boot.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ‘userService’ for bean class [package1.UserService] conflicts with existing,non-compatible bean definition of same name and class [package2.UserService]

我如何拥有2个同名服务?

解决方法

从类中删除@Qualifier,仅在自动装配时使用@Qualifier

@Service("kb")
public class UserService {
...
}

package package2;

@Service("user")
public class UserService {
...
}

来自@Qualifier javadoc

**
 * This annotation may be used on a field or parameter as a qualifier for
 * candidate beans when autowiring. It may also be used to annotate other
 * custom annotations that can then in turn be used as qualifiers.
 */

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读