gpt4 book ai didi

java - 具有相同类名(不同包)的两个 Spring 服务 Bean 即使使用限定符也会抛出错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:38:28 50 4
gpt4 key购买 nike

我有以下...

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,仅在 Autowiring 时使用@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.
*/

关于java - 具有相同类名(不同包)的两个 Spring 服务 Bean 即使使用限定符也会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50121981/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com