What I usually do in such cases is:
- inject the factory into the controller using
Spring
's bean
- create a method
getBaseCode(String code)
in the factory (please note: String
here stands for code type, so use the actual code type if not String
- make
getBaseCode
returning the BaseCode
interface while constructing the real implementation
- supposing you have an
execute
method in BaseCode
, use the getBaseCode
method into the controller to get the real collaborator and then call the execute
method to perform the actual action
Ignoring the first point (which I think you can easily looking at any Spring tutorial) the factory will be something like
public class BaseCodeFactory {
public BaseCode getBaseCode(String code) {
if(code.equals("something")) return new ThisBaseCodeImpl();
else //and so on
}
}
while computeDetails
becomes similar to:
@RequestMapping(method = RequestMethod.POST,value = "/test",headers="Accept=*/*")
public @ResponseBody ModelAndView computeDetails(@RequestBody RequestObj reqObj, ModelMap model) {
//...
factory.getBaseCode(reqObj.getcode()).execute();
//...
}
As a side note, I will not go for names like the one I choose here, I suggest you to look for something more significative in your domain (BaseCode
has no meaning for example), take this snippets just as a directive.
Base on OP comment. If you have ThisBaseCodeImpl
which makes use of other Spring bean you can
- annotate it with
@Configurable
so, when you use new ThisBaseCodeImpl(/*args if you like*/)
its bean are instantiated by Spring. I don't personally like this solution since, in my opinion, it pollutes the code with hidden Spring's bean. On the other hand is quite flexible, since it allows you to manage both runtime constructor arguments and Spring beans
- add
ThisBaseCodeImpl
to the Spring context and change the factory, so that a collaborator for ThisBaseCodeImpl
is injected into it.
1st point example:
@Configurable
public class ThisBaseCodeImpl {
@Resource
private Bean bean;
}
2nd point example:
public class BaseCodeFactory {
@Resource
ThisBaseCodeImpl thisBaseCodeImpl;
public BaseCode getBaseCode(String code) {
if(code.equals("something")) return thisBaseCodeImpl;
else //and so on
}
}