我有以下...

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;

但是当我尝试运行它时,我得到...



我如何拥有 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 Service Bean 也会抛出错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50121981/

10-10 20:10