Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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 动态创建用参数化类型扩展抽象类的Springbean_Java_Spring_Generics - Fatal编程技术网

Java 动态创建用参数化类型扩展抽象类的Springbean

Java 动态创建用参数化类型扩展抽象类的Springbean,java,spring,generics,Java,Spring,Generics,我希望利用Spring4.0对泛型类型自动连接的支持,但我希望避免为每种类型创建显式的具体类或匿名类。举个例子,假设我有一个接口: public interface Cache<T extends Entity> public abstract class AbstractCache<T extends Entity> implements Cache<T> { @Autowired private EntityDao<T> d

我希望利用Spring4.0对泛型类型自动连接的支持,但我希望避免为每种类型创建显式的具体类或匿名类。举个例子,假设我有一个接口:

public interface Cache<T extends Entity> 
public abstract class AbstractCache<T extends Entity> implements Cache<T>
{
    @Autowired
    private EntityDao<T> dao;

    @Autowired
    private List<CacheListener<T>> listeners;
    ...
} 
是否有一种方法可以通过
Cache
创建
Cache
的实例,以便在其他类中自动关联这些泛型类型?例如

@Autowire
private Cache<Z> zCache;
@Autowire
私有缓存;
我知道我可以通过单独定义每个bean来实现这一点,例如

@Bean
public Cache<Z> cacheZ() {
    return new AbstractCache<Z> () {};
}
@Bean
公共缓存cacheZ(){
返回新的AbstractCache(){};
}
但我一直无法为特定包中的所有实体类找到这样做的方法。例如

public void registerEntityCaches (BeanFactory beanFactory) {
    for (Class<? extends Entity> cls : entityPackage.getAllClasses()) {
         beanFactory.registerBean(new AbstractCache<cls>() {});
    }
}
公共无效注册表项缓存(BeanFactory BeanFactory){
为了(班级)
public void registerEntityCaches (BeanFactory beanFactory) {
    for (Class<? extends Entity> cls : entityPackage.getAllClasses()) {
         beanFactory.registerBean(new AbstractCache<cls>() {});
    }
}