我想根據請求參數在不同的服務對象上調用方法。我有這個目前..Spring的自動佈線服務
@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";
}
}
所以我在想,如果我可以使用反射樣的方法來調用正確的服務對象,而不是如果條件..我剛纔的意思是,我們有AService和BService實現共同的界面和實例化正確的對象,像這樣反映..
String classname = (String) request.getAttribute("classname");
Class classref = Class.forName(classname);
Constructor c = classref.getConstructor(null);
ServiceInterface sI = c.newInstance(null);
但隨着春天,我已經與自動裝配實例化對象,使有沒有什麼辦法來實現這一目標?
謝謝JB ...我現在要用map ... – RKodakandla