Announcement Announcement Module
Collapse
No announcement yet.
Spring's autowired service objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring's autowired service objects

    I want to call method on different service objects based on a request parameter. I have this currently..

    Code:
    @Controller
    public class HomeController {
    
    	@Autowired
    	AService aService;
    	
    	@Autowired
    	BService bService;
    	
    	@RequestMapping(value="home", method = RequestMethod.GET)
    	public String checkList(ModelMap modelMap, HttpServletRequest request){
    		String checkList = request.getParameter("listType");
                    if("listType" == "a")
      		       modelMap.addAttribute("list", aService.getList());
                    if("listType" == "b")
      		       modelMap.addAttribute("list", bService.getList());
    
    		return "checklist";
    	}
    }

    So I was wondering if I can use reflection kind of methodologies to call correct service object instead of if conditions.. I mean earlier, we had AService and BService implementing a common interface and instantiate correct object with reflection like this..

    Code:
                String classname = (String) request.getAttribute("classname");
                Class classref = Class.forName(classname);
                Constructor c = classref.getConstructor(null);
               ServiceInterface sI = c.newInstance(null);
    But with Spring, I already have the objects instantiated with AutoWiring so is there any way to achieve this?

  • #2
    Hello

    I think that Factory Pattern may be better for this case for creating ASevice or BService (but you still need to use if/else).

    You then use Autowired annotation (for that factory) in your controller without any logic (controller should not have "service" logic in it)

    Comment


    • #3
      I really hope that that isn't the code you are using (comparing strings with == ?!)...

      I would use a HashMap to map the list type to a bean name and then simply retrieve the needed bean from the application context. That way you can configure the mapping in xml.

      Code:
      @Controller
      public class HomeController {
      
      	private final Map<String, String> mappings = new HashMap<String, String>();
      	
      	@RequestMapping(value="home", method = RequestMethod.GET)
      	public String checkList(ModelMap modelMap, HttpServletRequest request){
      		String checkList = request.getParameter("listType");
      		String serviceName = mappings.get(checkList);
      		Service service = context.getBean(serviceName):
      		modelMap.addAttribute("list", service.getList());
      		return "checklist";
      	}
      	
      	public void setMappings(Map<String, String> mappings) {
      		this.mappings.clear();
      		this.mapppings.addAll(mappings);
      	}
      }
      If you use component-scanning to detect your Controllers this one needs to be put in xml and configure the mappings. If the mappings don't really change you could put them in a init method annotated with @PostConstruct, drawback is that you now have bean names in javacode.

      Comment

      Working...
      X