在搭建SSM框架时,整合业务逻辑层和表现层后一直报错, Servlet.init() for servlet springDispatcherServlet threw exception。Error creating bean with name ‘userController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory。
UserController代码如下
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
private static Logger log= LoggerFactory.getLogger(UserController.class);
@RequestMapping(value="/test",method= RequestMethod.GET)
@ResponseBody
public String test(HttpServletRequest request,Model model){
int userId = Integer.parseInt(request.getParameter("id"));
System.out.println("userId:"+userId);
User u = userService.getUserById(1);
System.out.println(u);
return u.toString();
}
}
通过IDEA的bean查找能够一直定位到配置文件。
最后发现,web.xml中引入配置contextConfigLocation有两种写法,全部写到servlet中的init-parm中就没有问题,部分写到context-parm中就会报上述错误。init-parm和context-parm的区别,网上给的说法是:context-param在所有的servlet中都能使用,init-param只能在当前的servlet中使用,如果不在当前的servlet中使用,取的值为null。
(1)报错的写法:
(2)正确的写法: