Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What I usually do in such cases is:</p> <ol> <li>inject the factory into the controller using <code>Spring</code>'s bean</li> <li>create a method <code>getBaseCode(String code)</code> in the factory (please note: <code>String</code> here stands for code type, so use the actual code type if not <code>String</code></li> <li>make <code>getBaseCode</code> returning the <code>BaseCode</code> interface while constructing the real implementation</li> <li>supposing you have an <code>execute</code> method in <code>BaseCode</code>, use the <code>getBaseCode</code> method into the controller to get the real collaborator and then call the <code>execute</code> method to perform the actual action </li> </ol> <p><br /> Ignoring the first point (which I think you can easily looking at any Spring tutorial) the factory will be something like</p> <pre><code>public class BaseCodeFactory { public BaseCode getBaseCode(String code) { if(code.equals("something")) return new ThisBaseCodeImpl(); else //and so on } } </code></pre> <p>while <code>computeDetails</code> becomes similar to:</p> <pre><code>@RequestMapping(method = RequestMethod.POST,value = "/test",headers="Accept=*/*") public @ResponseBody ModelAndView computeDetails(@RequestBody RequestObj reqObj, ModelMap model) { //... factory.getBaseCode(reqObj.getcode()).execute(); //... } </code></pre> <p>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 (<code>BaseCode</code> has no meaning for example), take this snippets just as a directive.</p> <p><strong>Base on OP comment</strong>. If you have <code>ThisBaseCodeImpl</code> which makes use of other Spring bean you can</p> <ol> <li>annotate it with <a href="http://docs.spring.io/spring/docs/3.2.5.RELEASE/javadoc-api/org/springframework/beans/factory/annotation/Configurable.html" rel="nofollow"><code>@Configurable</code></a> so, when you use <code>new ThisBaseCodeImpl(/*args if you like*/)</code> 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</li> <li>add <code>ThisBaseCodeImpl</code> to the Spring context and change the factory, so that a collaborator for <code>ThisBaseCodeImpl</code> is injected into it.</li> </ol> <p><strong>1st point</strong> example:</p> <pre><code>@Configurable public class ThisBaseCodeImpl { @Resource private Bean bean; } </code></pre> <p><strong>2nd point</strong> example:</p> <pre><code>public class BaseCodeFactory { @Resource ThisBaseCodeImpl thisBaseCodeImpl; public BaseCode getBaseCode(String code) { if(code.equals("something")) return thisBaseCodeImpl; else //and so on } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload