0

My application has a interface as below.

public interface MainInterface
{
     void someMethod();
}

Then, i have number of implementations of this interface.

@Service    
public class ImplClass1 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

@Service    
public class ImplClass2 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

@Service
public class ImplClass3 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

Below is a controller.

@Controller
public class MainController
{
     MainInterface implObj;

     @RequestMapping("service1")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass1();
     }

     @RequestMapping("service2")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass2();
     }

     @RequestMapping("service3")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass3();
     }
}

As mentioned in comment of each method i want to initialize it using spring. This is just an example. In my real time application i have 12 implementations of a interface and 6 methods in a controller.

Can you please guide how can i use autowire feature at method level or suggest any other best way around.

Thanks

Ankur Raiyani
  • 1,509
  • 5
  • 21
  • 49
  • I fear that, controller would be sigletone and you have made your interface an instance of the controller and you want to change implementation in each method. – Ravi Khakhkhar Jul 13 '12 at 20:35

1 Answers1

3

Can think of these two ways -

@Controller
public class MainController
{
     @Autowired @Qualifier("impl1") MainInterface impl1;
     @Autowired @Qualifier("impl2") MainInterface impl2;
     @Autowired @Qualifier("impl3") MainInterface impl3;

     @RequestMapping("service1")
     public void service1Handler()
     {
          impl1.doSomething()
     }

     @RequestMapping("service2")
     public void Service1Handler()
     {
         //Replace below with @Autowire
          impl2.doSomething()
     }

     @RequestMapping("service3")
     public void Service1Handler()
     {
         //Replace below with @Autowire
           impl3.doSomething()
     }
}

OR hide it behind a factory:

class MaintenanceInterfaceFactory{
     @Autowired @Qualifier("impl1") MainInterface impl1;
     @Autowired @Qualifier("impl2") MainInterface impl2;
     @Autowired @Qualifier("impl3") MainInterface impl3;
     getImplForService(String name){
        //return one of the impls above based on say service name..
     }
}
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Thanks for your reply. As i said i have 12 implementations of interface so it would be difficult to create 12 class variable according to first option. But your i may follow your second solution. Thanks again !!! – Ankur Raiyani Jul 17 '12 at 07:02