I would like to use Spring 4.0 support to auto-update generic types, but I would like it to be unnecessary to create explicit concrete or anonymous classes for each type. To use the example, let's say I have an interface:
public interface Cache<T extends Entity>
And an abstract interface implementation:
public abstract class AbstractCache<T extends Entity> implements Cache<T>
{
@Autowired
private EntityDao<T> dao;
@Autowired
private List<CacheListener<T>> listeners;
...
}
And entity classes A through Z that implement Entity (for example):
public class A implements Entity
public class B implements Entity
...
public class Z implements Entity
Is there a way to instantiate Cache<A>through Cache<Z>so that I can autwire these common types in other classes? For example.
@Autowire
private Cache<Z> zCache;
I know that I can achieve this by individually defining each bean, for example.
@Bean
public Cache<Z> cacheZ() {
return new AbstractCache<Z> () {};
}
But I was unable to find a way to do this for all Entity classes in a specific package. For example.
public void registerEntityCaches (BeanFactory beanFactory) {
for (Class<? extends Entity> cls : entityPackage.getAllClasses()) {
beanFactory.registerBean(new AbstractCache<cls>() {});
}
}
- ?