在我的项目中,我有以下存储库结构:
public interface IRepository<T> { ... }
public abstract class AbstractRepository<T> implements IRepository<T> { ... }
@Repository
public class ARepository extends AbstractRepository<A>
implements IRepository<A> { ... } // added line
@Repository
public class BRepository extends AbstractRepository<B>
implements IRepository<B> { ... } // added line
@Repository
public class CRepository extends AbstractRepository<C>
implements IRepository<C> { ... } // added line
现在我想将它们注入(inject)到适当的服务中,如下所示:
public class MyServiceImpl implements MyService {
@Autowire
IRepository<A> arepository;
...
}
它编译正常,但是当 Spring 上下文启动时,我收到以下错误 No qualifying bean of type 'some.package.IRepository<?>' available: expected single matching bean but found 3: aRepository, bRepository, cRepository
.
据我所知,Spring 4 中的注入(inject)(4.3.13.RELEASE 在我的项目中使用)应该通过使用泛型类型作为一种限定符形式来很好地处理这种情况。不幸的是它不适合我。我是否也应该更改一些配置?我对该主题进行了一些搜索,但没有发现任何有用的信息。我已经停止使用我的 bean 和 @Qualifier
的名称。注释,但对于该解决方案,我仍然依赖于实现。
有没有人遇到过类似的问题并设法解决了?
@编辑
之前没有提到,但正如一条评论表明它可能与此有关,我必须写这个项目使用方面(CTW)并且由 ajc
构建编译器。
最佳答案
我用下面的代码测试了这个:
public interface MyInterface<T> {
}
public abstract class MyAbstract<T> implements MyInterface<T> {
}
@Service
public class MyServiceInteger extends MyAbstract<Integer> implements MyInterface<Integer> {
}
@Service
public class MyServiceString extends MyAbstract<String> implements MyInterface<String> {
}
//in a service
@Inject
private MyInterface<String> stringSomething;
而且效果很好。有一次我确实收到了一条与您类似的错误消息,但这是由于在两种服务中使用了相同的通用参数 (String)。您是否 100% 确定您正在使用(仅)Spring 4.x?检查你的类路径..
关于java - Spring:通用注入(inject)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48442580/