简体   繁体   English

对泛型的依赖注入

[英]Dependency Injection to generics spring

Is it possible to Inject generic in spring boot like below? 是否可以像下面这样在Spring Boot中注入Generic?

public interface A <T> extends ElasticsearchRepository<T, Long> {

}

public class B {
    @Autowired
    private A<Object> a;

}

following code give this error 以下代码给出此错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericElasticRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unable to obtain mapping metadata for class java.lang.Object!

is there any alternatives to generic? 通用有替代品吗? I dont want to write for interfaces for all type of T 我不想为所有类型的T编写接口

thanks in advance 提前致谢

updated 更新

There was not any problem with generics in my code I guess it is rather repositories has something to do with concrete class. 我的代码中的泛型没有任何问题,我想它是存储库与具体类有关。 JpaRepository or ElasticsearchRepository does not allow to pass generic they need concrete type I dont know why JpaRepository或ElasticsearchRepository不允许传递泛型,它们需要具体的类型,我不知道为什么

With Spring 4.0 you can use generics it will automatically consider generics as a form of @Qualifier. 在Spring 4.0中,您可以使用泛型,它将自动将泛型视为@Qualifier的一种形式。 Reference - https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics 参考-https: //spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM