Spring:通用注入不起作用

Dco*_*tez 5 java generics spring dependency-injection

在我的项目中,我有以下存储库结构:

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 
Run Code Online (Sandbox Code Playgroud)

现在我想将它们注入到适当的服务中,如下所示:

public class MyServiceImpl implements MyService {

    @Autowire
    IRepository<A> arepository;

    ...
}
Run Code Online (Sandbox Code Playgroud)

它编译得很好,但是当 Spring 上下文开始时,我收到以下错误No qualifying bean of type 'some.package.IRepository<?>' available: expected single matching bean but found 3: aRepository, bRepository, cRepository

据我所知,Spring 4 中的注入(我的项目中使用了 4.3.13.RELEASE)应该通过使用泛型类型作为限定符的形式来很好地处理这种情况。不幸的是,它对我不起作用。我也应该更改一些配置吗?我对该主题进行了一些搜索,但没有发现任何有用的信息。我已经结束为我的 bean 和@Qualifier注释使用名称,但使用该解决方案我仍然依赖于实现。

有没有人遇到过类似的问题并设法解决它们?

@Edit 之前没有提到,但正如一条评论表明它可能与此有关,我必须写到这个项目使用方面(CTW)并且由ajc编译器构建。

Tob*_*obb 0

我用以下代码对此进行了测试:

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;
Run Code Online (Sandbox Code Playgroud)

而且效果很好。我确实曾经收到过像您一样的错误消息,但这是由于在两个服务中使用相同的通用参数(字符串)造成的。您是否 100% 确定您正在使用(仅)Spring 4.x?检查你的类路径..