关于@Autowired注解扫描不到

expected at least 1 bean which qualifies as autowire candidate

需求:在旧的SSM项目中,经常会出现找不到Bean的情况,然后会报红色下划线
解决方式:检查配置,检查注解

1
2
@Autowired
private CommonService commonservice;

1.检查配置文件

检查spring配置文件是否有去扫描包(如果少了这一步就报错找不到Bean)

1
2
<!-- 自动扫描 -->
<context:component-scan base-package="com.foam"/>

2.是否有注解

比如如service,repository,component,controller,Mapper等。

1
2
3
4
5
6
7
8
@Repository
public interface CompanyMapper{
……
}
@Service
public class Service{
……
}

3.是否有配置监听器

1
2
3
4
5
6
7
8
9
10
<!-- 上下文配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-*.xml</param-value>
</context-param>

<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

参考文章