Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用泛型java的类型参数类_Java_Micronaut - Fatal编程技术网

使用泛型java的类型参数类

使用泛型java的类型参数类,java,micronaut,Java,Micronaut,我正试图使用Java15中的泛型类和下面的代码 @Singleton public class GenericRepository<T> implements IGenericRepository<T>{ private final MongoClient mongoClient; public GenericRepository(MongoClient mongoClient) { this.mongoClient = mong

我正试图使用Java15中的泛型类和下面的代码

@Singleton
public class GenericRepository<T> implements IGenericRepository<T>{
    private final MongoClient mongoClient;
    
    public GenericRepository(MongoClient mongoClient) {
        this.mongoClient = mongoClient;
    }

    public MongoCollection<T> getCollection(String collectionName) {
        return mongoClient
                .getDatabase("main")
                .getCollection(collectionName, T.class);
    }
}
@Singleton
公共类GenericRepository实现了IGenericRepository{
私人最终MongoClient MongoClient;
公共一般报告(MongoClient MongoClient){
this.mongoClient=mongoClient;
}
公共MongoCollection getCollection(字符串collectionName){
返回mongoClient
.getDatabase(“主”)
.getCollection(collectionName,T.class);
}
}
我不能使用t.class,我如何解决这个问题

我找到的解决办法

@Singleton
public class GenericRepository<T> implements IGenericRepository<T>{
    private final MongoClient mongoClient;
    private final Class<T> typeParameterClass;

    public GenericRepository(MongoClient mongoClient, Class<T> typeParameterClass) {
        this.mongoClient = mongoClient;
        this.typeParameterClass = typeParameterClass;
    }

    public MongoCollection<T> getCollection(String collectionName) {
        return mongoClient
                .getDatabase("main")
                .getCollection(collectionName, this.typeParameterClass);
    }
}
@Singleton
公共类GenericRepository实现了IGenericRepository{
私人最终MongoClient MongoClient;
私有最终类typeParameterClass;
公共GenericRepository(MongoClient MongoClient,类typeParameterClass){
this.mongoClient=mongoClient;
this.typeParameterClass=typeParameterClass;
}
公共MongoCollection getCollection(字符串collectionName){
返回mongoClient
.getDatabase(“主”)
.getCollection(collectionName,this.typeParameterClass);
}
}

因为使用这个解决方案需要额外的代码,有没有更好的方法来实现这一点?

一个单例类,它唯一的构造函数接受参数,并且是参数化的

你的代码毫无意义。你遇到的问题在其他情况下是重要的,但这次不是。这个问题没有直接的解决方案(问题是:泛型被删除),但是有不同的代码样式可以避免它

问题是,因为这个问题甚至不适用于这种情况,所以很难解释如何重写这个代码,所以这个问题作为一个一般原则消失了

在这里,你只是。。。删除类型param,去掉该接口(一般来说,如果您有
IFoo
挂起,有些地方不对劲),就这么简单

如果你想推广这个概念,这样你就可以做很多(比如说你有一个类来检索foo,另一个类来检索条),你可以在公共类foofeatcher implements genericfeatcher中提取
,虽然这有点棘手(您可以从自己的
java.lang.Class
中使用
getGenericSuper
,然后从那里开始使用它。在这种情况下有很多警告,所以我不会对此进行进一步的扩展,只知道您可以这样做


如果在不同的情况下,您确实需要以运行时可查询的方式传递泛型,那么您的样式的问题是
类对象
泛型参数
重叠但不相同。
int
有一个类对象(
int.class
),但
List
不是有效的java代码。
List
是有效的泛型(
List x;
是有效的java),但是
List.class
不是一件事,也永远不会是,只有
List.class
可以是。
?扩展映射泛型在Java中运行时被擦除,这就是为什么不能使用
t.class
。您找到的解决方案通常就是使用的解决方案(但请注意,即使解决方案也不是完全类型安全的,例如,
列表
不能完全由
对象表示)。不是在vanilla Java中。您是否碰巧使用了Spring?我使用的是Micronaut framework,很抱歉没有使用springNote。对我来说,将泛型类型用作单例似乎很奇怪。它是泛型的事实表明了不同的参数化,但将其设为单例只表明了一个参数化(在这种情况下,为什么将该类型设为泛型?)。撇开@Slaw关于在单例中使用泛型的评论不谈,您找到的解决方案似乎是这样做的正确方法。与其将其传递给构造函数,还不如将其传递给正在进行类型转换的方法。在这两者之间,最好考虑一种将
mongoClient
配置为使用
main
作为数据库的方法se并调用它的
getCollection
。这将完全消除整个类