How to auto-create non-existing beans during injection?
Hi!
I'm trying to do some "dynamic" injection using Spring. What the heck I mean by dynamic? I have a base abstract Dao class that has a generic type (which means the Model) nad has all basic (and even some advanced) persistence operations. Something like this:
Code:
public abstract class Dao<M extends PersistentModel> {
// a bunch of methods: insert, update, findAll, findByExample, etc
}
The problem is, for simple CRUD operations I have to create "empty" Daos like this:
Code:
@Repository
public class ProductDao extends Dao<Product> {
// no methods needed
}
So, I want to achieve the following: if a Dao is not present for some model, create an anonymous implementation automatically and make it available for injection.
Suppose this service class and that the ProductDao above does not exist:
Code:
@Service
public class ProductService {
@Autowired
private Dao<Product> dao; // the bean is not found, create a new repository implementation and inject
}
Did you guys get it? Can someone point me in the right direction? I'm looking at BeanFactory, FactoryBean, PostProcessor but no success until now.
Thanks in advance!