1

I need to dynamically get a service bean in a controller from a factory method. I have a service interface

public interface Service{
     public String doSomething();
}

and some implementation of this interface.

public class ServiceImpl1{
    public String doSomething(){
      ...
    }
}

public class ServiceImpl2{
    public String doSomething(){
      ...
    }
}

There are 20 such implementations.

Now, in my controller I want to get one of these service impl instance, with dependency injected, based on user request. I am thinking of using a factory method to give me the instance, but to get dependency injected bean I need to load the context and get the bean. Is there any better way of doing it in Spring.

msmani
  • 730
  • 1
  • 7
  • 24
  • 1
    As a rule of thumb NEVER load the context as that will basically load the whole application again (in general). Unless you want to run into performance issues, transaction issues and weird concurrency issues. The `ApplicationContext` is already a factory why not just use that. Or make your own factory also spring bean, inject that tinto your controller and put the logic for deciding which one to use in the factory. – M. Deinum Jun 17 '15 at 06:28
  • If the factory uses the bean name to know which bean to return, then just get the bean from the spring context. If some other criterion is used, you can inject a List into your factory, and decide which of the elements to return the way you want. – JB Nizet Jun 17 '15 at 06:32
  • @M.Deinum I am using my factory as a spring bean, but the problem is the factory has to return one of the 20 implementations, if the service implementation is stateless then I can inject all the 20 implementations to the factory and return one as per the need, but what happens in a scenario where the service impl is stateful, I know that it is not the right way to keep service layer stateful, then it has to be of prototype scope and we can't inject the bean to factory at load time. How to proceed in this case. – msmani Jun 17 '15 at 07:26
  • As stated the `ApplicationContext` is already a factory. Don't inject the beans, instead do a lookup when you need them. – M. Deinum Jun 17 '15 at 07:30
  • @M.Deinum but this [answer](http://stackoverflow.com/questions/812415/why-is-springs-applicationcontext-getbean-considered-bad) says that it is a bad practice to get bean from application context – msmani Jun 17 '15 at 07:36
  • As a general rule of thumb, in this case it isn't. There is no other way to obtain a fresh instance of the prototype scoped bean. – M. Deinum Jun 17 '15 at 07:45
  • @M.Deinum ok, in case of singleton bean should I inject all beans to factory and return one as per request or in my factory should I get the bean from application context and return it,instead of injecting all beans. which is recommended – msmani Jun 17 '15 at 07:51
  • Whatever you want. You cannot inject a prototype scoped bean as that would basically make it a singleton. I wouldn't mix different approaches (i.e. partially inject beans otherwise not) and stick with a single one. But that is highly opinionated. – M. Deinum Jun 17 '15 at 07:53
  • @M.Deinum ok, thanks – msmani Jun 17 '15 at 07:56

0 Answers0