Could not autowire. No beans of ‘xxxx‘ type found

问题描述

  在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示,但程序的编译和运行都是没有问题的,这个错误提示并不会产生影响。

原因

 原因可能有两个,第一个是idea本身工具的问题。第二个便是我们导入@Service包的时候导入包错误造成的。

  第一种原因,spring auto sacn配置,在编辑情况下,无法找到对应的bean,于是提示找不到对应bean的错误。常见mybatis的mapper,如下:

<!-- mapper scanner configurer-->
<bean id="mapperScannerConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.adu.spring_test.mybatis.dao"/>
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

解决方案

  针对第一种原因,解决办法是:降低Autowired检测的级别,将Severity的级别由之前的error改成warning或其它可以忽略的级别。

  针对第二种原因,解决方案当然是导入正确的包。首先我们来看下最容易导入的错误包,如下:

//错误导入方式
import com.alibaba.dubbo.config.annotation.Service;

//正确导入方式
import org.springframework.stereotype.Service;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Could not autowire. No beans of 'Integer' type found的错误提示意味着在自动装配时找不到'Integer'类型的bean。这通常是由于没有在应用程序的上下文中定义或配置'Integer'类型的bean所致。要解决此问题,可以采取以下步骤: 1. 检查配置文件:确保在应用程序的配置文件(如application.properties或application.yml)中定义了正确的bean。例如,在Spring Boot应用程序中,可以使用@Component或@Repository注释在对应的类上定义bean。 2. 检查包扫描:确保扫描到包含'Integer'类型bean的包。在Spring应用程序中,默认情况下会扫描与主应用程序类相同的包及其子包。如果您的bean位于不同的包中,可以通过@Configuration注释的类中使用@ComponentScan指定要扫描的包。 3. 检查依赖注入:如果您正在使用@Autowired注释进行依赖注入,请确保@Autowired注释的字段或方法的类型与所需的'Integer'类型匹配。另外,也可以尝试使用@Qualifier注释指定要注入的特定bean。 4. 检查bean的定义:如果您手动定义bean,可以确保在配置文件(如XML配置文件)中正确地定义了'Integer'类型的bean。确保bean的名称与@Autowired注释中使用的名称匹配。 总结:Could not autowire. No beans of 'Integer' type found的错误提示表明在自动装配过程中找不到'Integer'类型的bean。通过检查配置文件、包扫描、依赖注入和bean的定义,您可以解决此问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [IntelliJ Idea错误提示 Could not autowire. No beans of ‘‘ type found](https://blog.csdn.net/wangshuminjava/article/details/122616690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示](https://download.csdn.net/download/weixin_38632624/12751965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值