项目场景:
搭建ssm框架实现简单的数据库查询功能
问题描述:
搭建ssm框架时出现Service层注入失败问题
原因分析:
起初认为是jetty服务器的问题,后来换成tomcat7服务器问题仍然未解决,尝试将Service层中代码去除,可以正常运行。查询百度发现问题可能来源于以下几个方面:
- @Service注解是否标注
- spring.xml文件是否开启自动扫描
<context:component-scan base-package="com.shsxt.jackson">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>`
- web.xml文件是否加载spring.xml以及ContextLoaderListner
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- @Resource注解是否引入依赖以及依赖是否正确
解决方案:
排除前三个问题后,锁定到@Resource注解问题,使用@Autowired注解后成功解决问题,原来是@Resource注解的api引入错误。