0
我的应用程序具有如下的接口。方法内的Spring Autowire
public interface MainInterface
{
void someMethod();
}
然后,我有这个接口的实现数量。
@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
}
}
下面是一个控制器。
@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();
}
}
正如在每个方法的评论中提到的,我想用spring初始化它。 这只是一个例子。在我的实时应用程序中,我在控制器中有12个接口实现和6个方法。
可以请指导我如何在方法级别使用autowire功能或建议任何其他最佳方法。
感谢
我担心的是,控制器会sigletone,你让你的界面控制器的一个实例,并且您想要在每种方法中更改实现。 – 2012-07-13 20:35:57
是的,这是真的。 – 2012-07-17 07:05:03