org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type **

spring 工程运行时报错:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ***: Unsatisfied dependency expressed through field ***; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type *** available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

先把以下可能的原因都排除了:
1. 没写 @Service 之类的 bean 声明注解,这个 IDE 就会检测出来并提示错误;
2. 没在上下文配置文件中声明 <context:component-scan base-package="" />,这个 IDE 也能检测出来;
3. bean 所在的位置不在 component-scan base-package 下;

最后检查 web.xml 配置文件发现了问题所在:
因为上述 bean 我都是在 额外的上下文配置 中导进来的,如果启用了 额外的上下文配置,类似:

<!-- Extra context configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-*.xml</param-value>
    </context-param>

那么相对应的必须启动 ContextLoaderListener – 上下文装载监听器,也就是要配置:

<!-- Setup ContextLoaderListener.
        Attention: without this, all context cannot be loader in context-param -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

否则,你<context-param>在配置的所有上下文都不会被导进来,而且这个 IDE 检测不出来,只会在程序运行的时候才报上述的错误。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值