Eclipse 使用svn插件检出maven项目,启动tomcat服务器时提示依赖注入失败
控制台错误信息
No qualifying bean of type [xxx.xxx] found for dependency: expected at least 1 bean
which qualifies as autowire candidate for this
dependency.Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决步骤
1.eclipse 检出后 maven 项目后,右键项目名,在右键菜单中点击configure
选择 convert to maven project
2.右键项目名,点击Build Path
选择 Configure Build Path...
3.在Java Build Path
的Source
面板中,点击右侧的Add Folder...
4.在Source Folder Selection
弹窗中勾选resource
文件夹前的复选框
5.重新编译项目、部署到tomcat中,启动tomcat,项目可以正常访问
原因分析
先看下web.xml
中关于spring的配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
ContextLoaderListener
加载的配置文件是类路径(即classes
)下的applicationContext.xml
DispatcherServlet
加载的配置文件是WEB-INF
下的spring-mvc.xml
前面项目启动的时候报错,是因为resources
文件在编译、部署的时候并没有被加载到/WEB-INF/classes
路径下,导致ContextLoaderListener
并没有成功加载applicationContext.xml
,以至于依赖的bean
未生成,而无法注入