Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 Spring获取泛型类型类_Java_Spring_Generics - Fatal编程技术网

Java Spring获取泛型类型类

Java Spring获取泛型类型类,java,spring,generics,Java,Spring,Generics,我知道这个问题经常被问到,但我找不到解决办法。 如何在Spring注入的存储库中获取泛型类型类名 这是我的基本存储库 public interface UserRepository extends JpaRepository<User, Long>, IUserRepository<User>{ User findByUsername(String username); } public interface UserRepository扩展了JpaReposito

我知道这个问题经常被问到,但我找不到解决办法。 如何在Spring注入的存储库中获取泛型类型类名

这是我的基本存储库

public interface UserRepository extends JpaRepository<User, Long>, IUserRepository<User>{
   User findByUsername(String username);
}
public interface UserRepository扩展了JpaRepository、IUserRepository{
用户findByUsername(字符串用户名);
}
这是接口

public interface IUserRepository<T> {
   public List<T> findAllValidEtSiteAndStructure();
}
公共接口存储库{
公共列表findAllValideSiteAndStructure();
}
最后是实现

public class UserRepositoryImpl<T> implements IUserRepository<T> {

@PersistenceContext
private EntityManager em;

private Class< T > type;

@Override
public List<T> findAllValidEtSiteAndStructure() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof UserAuthentication) {
        final User currentUser = ((UserAuthentication) authentication).getDetails();
        return (List<T>) em.createQuery("FROM " + type.getName()+ " WHERE site=:site AND structure=:structure AND valid=:valid")
                .setParameter("site", currentUser.getInstallation().getSite())
                .setParameter("structure", currentUser.getInstallation().getStructure())
                .setParameter("valid", true)
                .getResultList();
    }
    return null;
}
}
公共类UserRepositoryImpl实现IUserRepository{
@持久上下文
私人实体管理者;
私有类型;
@凌驾
公共列表FindAllValideSiteAndStructure(){
最终身份验证=SecurityContextHolder.getContext().getAuthentication();
if(用户身份验证的身份验证实例){
最终用户currentUser=((用户身份验证)身份验证).getDetails();
return(List)em.createQuery(“FROM”+type.getName()+“其中site=:site AND structure=:structure AND valid=:valid”)
.setParameter(“站点”,currentUser.getInstallation().getSite())
.setParameter(“结构”,currentUser.getInstallation().getStructure())
.setParameter(“有效”,true)
.getResultList();
}
返回null;
}
}
如何获取type.name?
提前感谢

您的问题的一般答案可以在“Spring数据存储库的自定义实现”一章的文档->中找到

但我认为这在你的情况下是不必要的。你应该能够用下面的方法来做

public interface UserRepository extends JpaRepository<User, Long> {

   User findByUsername(String username);

   List<User> findByStructureAndSiteAndValid(Structure structure, Site site, boolean valid);
}
public interface UserRepository扩展了JpaRepository{
用户findByUsername(字符串用户名);
列出FindBysttructureAndSiteAndValid(结构结构、站点、布尔值有效);
}

基本上您无法获取泛型类型,因为

我要做的是向
UserRepositoryImpl
添加一个抽象方法,该方法返回相关类型:

public abstract Class getType();
然后,我将为
UserRepositoryImpl
创建特定实例,编译时已经知道其类型。例如:

public class StudentRepository extends UserRepositoryImpl<Student> {
  public Class getType() {
    return Student.class;
  }
}
public类StudentRepository扩展了UserRepositoryImpl{
公共类getType(){
返回学生班级;
}
}

考虑到您使用的是Spring框架,请使用下面的代码片段,我已经测试过了,效果很好:

ResolvableType resolvableType = ResolvableType.forClass(UserRepository.class).as(JpaRepository.class);
System.out.println(resolvableType.getGeneric(0));//User
System.out.println(resolvableType.getGeneric(1));//Long