@当spring从3.2升级到4.2时,通用类型的Autowired不起作用



我正在我的球衣实现的RESTful web应用程序中将spring从3.2升级到4.2,其中spring用作DI。我有以下类层次结构:

interface Representor<S> {...}
@Component
class MyRepresentor implements Representor<SomeType> {...}
class Foo implements SomeType {...}
class Bar implements SomeType {...}
@Component
MyResource {
  @Autowired
  private Representor<Foo> representor;
}

private Representor<Foo> representor给了我一个编译错误Could not autowire. No beans of 'Representator<Foo>' type found。它曾经与Spring 3.2 配合良好

编辑:

我试过这个:

  @Autowired
  @Qualifier("myRepresentor")
  private Representor<Foo> representor;

还有这个:

 @Compenent
 interface Representor<S> {...}

它们都解决了编译错误,但导致了运行时错误No qualifying bean of type [a.b.Representor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

我通过向MyRepresentor类添加泛型类型来解决此问题。

@Component
class MyRepresentor<T> implements Representor<SomeType> {...}

用@Component注释您的界面。希望它能起作用。

最新更新