【发布时间】:2011-09-28 00:31:18
【问题描述】:
我正在尝试在 MVC 控制器类中自动装配一个 bean,但我无法获得另一个值,而不是 null。当我将 throw new Error("E") 放入要注入 helloWorldController bean 的 bean 的构造函数中时,我得到一个异常:Error creation bean with name helloWorldController: Injection of autowired dependencies failed. 但是当我运行在构造函数中没有 error 的测试,我没有得到 bean,我得到 null。
我完全糊涂了。它有什么作用?它在创建控制器实例时尝试创建和注入依赖项。好的,如果没有发生错误,为什么变量没有初始化?
在肖恩·帕特里克·弗洛伊德 (Sean Patrick Floyd) 的要求下,我延长了我的职位:
包 testy.sprung; //导入声明省略 导入 testy.sprung.beany.AwiredBean; @控制器 公共类 HelloWorldController { 私人 Logger log = Logger.getLogger("springTestLogger"); @自动连线 私人 AwiredBean 盎司; @RequestMapping("/sprung") 公共模型和视图基础(){ log.debug("基础 URI"); ModelAndView mv = new ModelAndView(); mv.setViewName("firstPage"); 返回 mv; } @RequestMapping(value="/{articel}/{subTitle}",method=RequestMethod.GET) public ModelAndView szia(@PathVariable("articel") String articel, @PathVariable("subTitle") String st, @RequestParam(value="co", required=false) String co) { log.debug("路径 GET/{articel}/{subtitle}:" + articel + "/" + st + "?co=" + co); ModelAndView mv = new ModelAndView(); mv.setViewName("索引"); // 现在将 index.jsp 放入 /WEB-INF/files mv.addObject("articel", articel); mv.addObject("subtl", st); mv.addObject("co", co); mv.addObject("awir", oz); //但它是空的 返回 mv; } }bean 实现任何空接口:
包 testy.sprung.beany; 公共类 AwiredBeanImpl 实现 AwiredBean { @覆盖 公共字符串 toString() { 返回“CommonAutowired”; } 公共 AwiredBeanImpl() { 抛出新错误(“E”); } }我在测试中运行它。测试失败,因为我的错误或NullPointerException 被抛出:
【问题讨论】:
-
请显示控制器类和spring config的相关位
标签: model-view-controller spring autowired