项目中spring容器加载的问题

今天做一个项目采用的是传统架构,没有采用分布式,部署时出现了异常,信息是:

org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type found for dependency

error creating bean with name 'xxx': cannot resolve refere 'xxx'

expected at least 1 bean which qualifies as autowire candidate for this dependency

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

无法创建ItemService,因为找不到

首先检查ItemService的实现类上是否加了@Service注解,发现加了

@Service
public class ItemServiceImpl implements ItemService {

然后检查spring的配置文件是否有注解扫描器,发现有

 <!--注解扫描器-->
    <context:component-scan base-package="com.rui.service"/>

 

最后检查web.xml是否加载了spring容器,发现没有,于是添加代码

  <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:com.rui/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  

错误原因找到了,在web.xml中仅配置了springmvc的前端控制器,没有加载spring容器。

猜你喜欢

转载自www.cnblogs.com/a154627/p/9991370.html
今日推荐