During object injection, the type of pair is generic, @Autowired can inject normally, but @Resource cannot inject normally?
@Resource :
public class ServiceImpl<R extends BaseRepository, T extends BaseEntity> {
@Resource
private R repository;
.........
}
Error Info: nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type '?' available: expected single matching bean but found 3: .........
@Autowired
public class ServiceImpl<R extends BaseRepository, T extends BaseEntity> {
@Autowired
private R repository;
.........
}
This is true.
My Spring Boot version:2.6.6
Comment From: snicoll
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
@Resource
uses by-name semantic, see the documentation.
Comment From: my420840806
According to the @Resource document, if the name does not match, it will match according to the type, but it does not match according to the @Autowired type, which does not match its definition. My suggestion is to improve the logic of @Resource underlying injection to enable it to correctly identify generic types. Thank you!
Comment From: snicoll
I don't know what documentation you are referring to but the fallback on type when no name is provided is best efforts. You should not be using it if the type matters like in your example.