Dependency Injection, need factory like functionality
I'm developing a Web application using Spring.
I have a common abstract class and many implementions of it.
Now, in a controller, depending on some parameter, I need different
implementations of it.
This can be easily implemented with Factory Pattern.
For example:
abstract class Animal{
public abstract void run(){
}
}
class Dog extends Animal{
...
}
Class Cat extends Animal{
...
}
Now with factory pattern, I can create a factory class with a factory
method which creates Animals based on some parameter. But I don't want to
create instances on my own.
I need the same functionality, but I want Spring to manage everything.
Because different implementations have their dependencies and I want them
to be injected by Spring.
What is the best way of handling this situation?
No comments:
Post a Comment