Results 1 to 3 of 3

Thread: Spring's autowired service objects

  1. #1
    Join Date
    Aug 2011
    Posts
    24

    Default 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. #2
    Join Date
    Apr 2006
    Posts
    166

    Default

    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)

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •