【Spring常见错误】No qualifying bean of type

这篇具有很好参考价值的文章主要介绍了【Spring常见错误】No qualifying bean of type。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

报错信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ssmpdemo.ServiceTest': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssmpdemo.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssmpdemo.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
 

对症下药:

很明显,错误原因为SpringBoot自动注入Bean找不到相对应的Bean类型。导致自动注入失败。

既然发现错误就对症下药,找对应的Bean,这里找的是Service对应的实现类

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssmpdemo.entity.User;
import com.ssmpdemo.mapper.UserMapper;
import com.ssmpdemo.service.UserService;

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

发现对应的实现类开头未加注解:(如果注解已加,可在相关问题找找是否有对应的问题)

问题解决:

@Controller、@Service、@Repository、 @Component 加上其中一个。

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssmpdemo.entity.User;
import com.ssmpdemo.mapper.UserMapper;
import com.ssmpdemo.service.UserService;
import org.springframework.stereotype.Repository;

@Repository
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

相关问题:

包目录不对应

例如:Application所在包:com.spring.demo

这时Spring只会扫描demo目录下的所有文件,如果需要的Bean不在此目录下,自然注入失败。

只需将Bean移动到Application所在目录下即可。

🙊🙊作者主页:🔗求不脱发的博客

📔📔 精选专栏:🔗SSM直击大厂

📋📋 精彩摘要:MyBatis 核心配置文件(xxxConfig.xml),该文件配置了MyBatis的一些全局信息,,包含数据库连接信息和MyBatis运行时所需的各种特性,以及设置和响应MyBatis行为的一些属性。本文将深入浅出的介绍MyBatis核心配置文件中常用的标签配置。

💞💞觉得文章还不错的话欢迎大家点赞👍➕收藏⭐️➕评论💬支持博主🤞文章来源地址https://www.toymoban.com/news/detail-403983.html

到了这里,关于【Spring常见错误】No qualifying bean of type的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • No qualifying bean of type ‘org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate‘ a

    No qualifying bean of type ‘org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 提示找不到 ElasticsearchRestTemplate 写一个配置类注入

    2024年02月17日
    浏览(37)
  • 项目依赖问题导致No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more tha...

    A项目之前一直都是好好的,但是某天下午启动的时候报错,报错见下,报错原因是org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type \\\'org.apache.ibatis.session.SqlSessionFactory\\\' available: more than one \\\'primary\\\' bean found among candidates。  初步定位这个问题的时候眼睛一直

    2023年04月09日
    浏览(28)
  • SpringBoot - 在IDEA中经常发现:Could not autowire. No beans of ‘xxx‘ type found的错误

    错误描述 在SPRINGBOOT的项目中,使用IDEA时经常会遇到Could not autowire. No beans of ‘xxxx’ type found的错误提示,但是程序的编译和运行都没有问题,这个错误提示并不影响项目的生产。 解决方案

    2024年02月15日
    浏览(41)
  • Could not autowire. No beans of ‘DiscoveryClient‘ type found.

    一、导错了包 DiscoveryClient对应有两个包: org.springframework.cloud.client.discovery.DiscoveryClient; com.netflix.discovery.DiscoveryClient; 目前导入的包是: 改成第一个包,发现不再报红了。

    2024年02月11日
    浏览(34)
  • @Autowired报错Could not autowire. No beans of ‘XXX‘ type found

      IDEA中使用 @Autowired 报错 Could not autowire. No beans of \\\'XXX\\\' type found ,错误大致意思为:没有匹配到类型为XXX的bean。   个人觉得,注入 controller 的 service 虽然一般来说我们都是注入一个接口,但是该接口有实现类,并且使用 @Service 进行关联,所以注入类型应该也可以视为一

    2024年02月07日
    浏览(35)
  • idea报错:Could not autowire. No beans of ‘UserService‘ type found.

    点个关注,必回关 翻译:无法自动连线。未找到“用户服务”类型的服务类。 当报错之后idea会提示错误,不过程序的编译和运行都是没有问题的(这个错误提示不会产生任何印象) 解决方案 解决方案1: Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class

    2024年02月11日
    浏览(35)
  • idea报“Could not autowire. No beans of ‘UserMapper‘ type found. ”错解决办法

    idea具有检测功能,接口不能直接创建bean的,需要用动态代理技术来解决。 1.修改idea的配置 1.点击file,选择setting 2.搜索inspections,找到Spring 3.找到Spring子目录下的Springcore 4.在Springcore的子目录下找到code 5.把seyerity选项改成警告 2.修改代码 1,@Autowrited改为@Autowrited(required = false)

    2024年02月05日
    浏览(51)
  • IDEA提示找不到Mapper接口:Could not autowire.No beans of ‘xxxMapper‘ type found

    我们可以看到,上面的红色警告在提示我们,找不到 xxxMaper 这个类型的 bean。 为啥呢? 因为 @Mapper 这个注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是却和 Mybatis 关联不上。而且我们可以根据 @Autowried 源码看到,默认情况下,@Autowri

    2024年02月08日
    浏览(33)
  • 解决SpringBoot项目中的报错:Could not autowire,no beans of “XXX“ type found

    问题:找不到mapper注入的bean,如图   分析:注入mapper有两种方式:  第一种:在启动类中添加  @MapperScan        然后在mapper的类中添加  @Repository 注解 第二种方法:直接在各个mapper类中添加@Mapper注解,但是一定要注意导入正确的包,否则解决不了这个异常;  很多新手

    2024年02月08日
    浏览(41)
  • 【错误】A component required a bean of type ‘org.springframework.security.config.annotation.ObjectPostPr

    Description: A component required a bean of type \\\'org.springframework.security.config.annotation.ObjectPostProcessor\\\' that could not be found. Action: Consider defining a bean of type \\\'org.springframework.security.config.annotation.ObjectPostProcessor\\\' in your configuration.   描述: 组件需要“org.springframework.security.configannotation”类型的

    2024年02月13日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包