@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@ResponseBody
@RequestMapping("/show")
public String show(){
User user = userService.selectById(1001);
System.out.println(user);
System.out.println("123");
return "ok";
}
}
编写SSM项目时,测试dao层和Service层都没问题,但是启动项目之后报错
Servlet.init() for servlet [mvc] threw exception
原因:在springMVC中注入Service失败。
经过排查发现是web.xml中没有监听加载spring的工厂
解决办法
在web.xml中加入监听即可解决
<!-- 通过监听器用于加载spring的工厂 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean/bean-*.xml</param-value>
</context-param>