Spring 4泛型类,获取参数化类型 [英] Spring 4 generic class, get the parametrized type

查看:99
本文介绍了Spring 4泛型类,获取参数化类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中有一个泛型类,我想为注入的bean获取泛型T类。我知道 Java中的经典方式并阅读 Spring 4如何实现Java泛型。此外,我试图找到一个解决方案,使用 ResolvableType ,但没有任何作用。

  @Autowired 
GenericDao< SpecificClass> specificdao;






  public GenericDaoImpl< T> {

私人课程< T>类型;

public DaoImpl(){
this.type = ...?
}

public T findById(Serializable id){
return(T)HibernateUtil.findById(type,id);


$ / code $ / pre

有没有办法避免这种情况?

  @Autowired 
@Qualifier
GenericDao< SpecificClass> specificdao;






  @ Repository(specificdao)
public SpecificDaoImpl扩展了GenericDao< SpecificClass> {
public SpecificDaoImpl(){
//假设构造函数在GenericDao
super(this.getClass())
}
}

谢谢。

解决方案

好吧,如果我明白你的问题:你想达到什么是非常棘手的。



你可以使用 TypeTools ,然后执行下面的操作:

  import net.jodah.typetools .TypeResolver; 

public GenericDaoImpl< T> {

私人课程< T>类型;
$ b $ public GenericDaoImpl(){
Class<?> [] typeArguments = TypeResolver.resolveRawArguments(GenericDaoImpl.class,getClass());
this.type =(Class< T>)typeArguments [0];
}

public T findById(Serializable id){
return(T)HibernateUtil.findById(type,id);
}
}

但我仍然怀疑这是不是一件好事想法即可。因为通常你不想要:

  @Autowired 
GenericDao< SpecificClass> specificDao;

但是相反:

  @Autowired 
SpecificDao specificDao;

为什么?因为每个DAO几乎总是与其他DAO完全不同的方法。通用的唯一通用方法可能是: findById findAll save count 删除等。

所以从GenericDAO的子类化是最明显的方法,因为它允许你在一个具体的DAO中添加任何需要的方法。



BTW:你说你想要自己重新实现Spring Data功能。但请注意,以Spring的方式,您仍然需要创建具体的存储库。


Hi have a Generic class in Spring, and I would like to get the generic T type class for an injected bean. I know the classic way in Java and read how Spring 4 implements Java Generics. Also, I tried to find a solution using ResolvableType but nothing works.

@Autowired
GenericDao<SpecificClass> specificdao;


public GenericDaoImpl <T> {

    private Class<T> type;

    public DaoImpl () { 
         this.type = ...? 
    }

    public T findById(Serializable id) {
        return (T) HibernateUtil.findById(type, id);
    }
}

Are there any way to avoid this?

@Autowired
@Qualifier
GenericDao<SpecificClass> specificdao;


@Repository("specificdao")
public SpecificDaoImpl extends GenericDao<SpecificClass> {
      public SpecificDaoImpl () {
          // assuming the constructor is implemented in GenericDao
          super(this.getClass())
      }
}

Thanks.

解决方案

Ok, if I understand your question: what you want to achieve is very tricky.

You can use TypeTools and then do sth like:

import net.jodah.typetools.TypeResolver;

public GenericDaoImpl <T> {

    private Class<T> type;

    public GenericDaoImpl () { 
         Class<?>[] typeArguments = TypeResolver.resolveRawArguments(GenericDaoImpl.class, getClass());
         this.type = (Class<T>) typeArguments[0];
    }

    public T findById(Serializable id) {
        return (T) HibernateUtil.findById(type, id);
    }
}

But still I doubt if it's a good idea. Because usually you don't want:

@Autowired
GenericDao<SpecificClass> specificDao;

But instead:

@Autowired
SpecificDao specificDao;

Why? Because each DAO has almost always completely different methods than other DAOs. The only generic methods, common to all, are probably: findById, findAll, save, count, delete etc.

So subclassing from a GenericDAO is the most obvious way, as it allows you to add any needed method inside a concrete DAO.

BTW: You said that you want re-implement Spring Data functionality on your own. But please notice that in Spring way, you still have to create concrete repositories.

这篇关于Spring 4泛型类,获取参数化类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆