通用类型的自动装配不起作用[Spring 4+]

时间:2016-03-26 15:02:31

标签: java spring spring-mvc generics autowired

我正在使用Spring 4.2.4.RELEASE。

开展一个项目

我已经听说过Spring 4的新功能(特别是关于autowiring of generic types),当下面的代码没有被编译时我很困惑:

@Service
public interface AuthenticationService<T> { ... }

public class VKAuthenticationService implements AuthenticationService<VKToken> { ... }

@RestController
public class VKAuthenticationController {
   @Autowired
   private AuthenticationService<VKToken> service;
}

提前感谢你的帮助。

1 个答案:

答案 0 :(得分:3)

如何在@Service

上声明VKAuthenticationService
@Service(name="myService")
public class VKAuthenticationService implements AuthenticationService<VKToken> { ... }

并使用@Autowired@Qualifier注入

@RestController
public class VKAuthenticationController {
   @Autowired
   @Qualifier("myService")
   private AuthenticationService<VKToken> service;
}
相关问题