IntelliJ Idea解决Could not autowire. No beans of 'xxxx' type found的错误提示

1.问题描述

  在Idea的spring工程里,常常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示。但程序的编译和运行都是没有问题的,这个错误提示并不会产生影响。但红色的错误提示在有些有强迫症的程序员眼里,多多少少有些不太舒服。程序员

 

2. 缘由

        缘由可能有两个,第一个是IntellijIDEA自己工具的问题。第二个即是咱们导入@Service包的时候导入包错误形成的

  第一种缘由,spring auto scan配置,在编辑状况下,没法找不到对应的bean,因而提示找不到对应bean的错误。常见于mybatis的mapper,以下:spring

<!-- 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>

 

3. 解决方案

  针对第一种缘由,解决办法是:下降Autowired检测的级别,将Severity的级别由以前的error改为warning或其它能够忽略的级别。sql

       针对第二种缘由,解决方案固然是导入正确的包。首先咱们来看下最容易导入的错误包,以下所示:mybatis

 

import com.alibaba.dubbo.config.annotation.Service;
   正确的包应该是下面这个
 
import org.springframework.stereotype.Service;
相关文章
相关标签/搜索