Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC 3.0 - Service layer using annonations
    primarykey
    data
    text
    <p>In my Spring MVC application with a single Controller class,</p> <pre><code>@Controller public class MyController { @RequestMapping(method = RequestMethod.POST, value = "/*.htm") public myMethod{@RequestBody final MyRequestBean myRequest} } </code></pre> <p>My input is in the form of JSON and I use Jackson for converting it to Java object. Now, based on the property in the Java object, I want to send to appropriate service class. If it is,</p> <pre><code>myRequest.value == "1" -&gt; FirstService myRequest.value == "2" -&gt; SecondService </code></pre> <p>All these Service classes will be implementing a basic interface. I dont want to do this instantiation in my Java code. How can I move this to a configuration file. How can I inject these objects dynamically based on the request values. Please note that am using annotations. Am new to Spring.Please advise</p> <p><strong>EDIT: To include my solution</strong><br> In my dispatcher.xml,</p> <pre><code>`&lt;util:map id="myMap"&gt; &lt;entry key="service1" value="com.service.MyService1" /&gt; &lt;entry key="service2" value="com.service.MyService1" /&gt; &lt;/util:map&gt;` </code></pre> <p>In my controller,</p> <p><code>@Resource private Map myMap;</code></p> <p>and in the method inside controller,<br> <code>MyService myService = (MyService) Class.forName((String)myMap.get(myRequest.getValue())).newInstance(); System.out.println("My value" + myService.doService());</code></p> <p>As can be seen, have used Java reflection for getting the instance from the string returned from my XML. Is this the correct way?</p>
    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.
 

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