记录 @Autuwired
无法注入的原因,以及采用的解决方法
一、错误
idea 错误提示: (大概意思就是没有扫描这个bean)
Could not autowire. No beans of 'HrService' type found.
具体描述:
Description:
Field hrService in com.hjx.pmsysweb.config.SecurityConfig required a bean of type 'com.hjx.pmsys.service.HrService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.hjx.pmsys.service.HrService' in your configuration.
二、方法及原因
SpringBoot 项目的 Bean 装配默认规则是根据 Application 类所在的包位置从上往下扫描!
“ Application 类”是指 SpringBoot 项目入口类。这个类的位置很关键:
如果 Application 类所在的包为:com.boot.app,则只会扫描 com.boot.app 包及其所有子包,
如果 service 或 dao 所在包不在 com.boot.app 及其子包下,则不会被扫描!
即, 把Application类放到 dao、service 所在包的上级,com.boot.Application 知道这一点非常关键
个人原因:包的路径不统一,因为学习分模块来建项目,在创建时没有注意,以至于 Application 类所在包名,没能与 Service 模块包名统一。
修改后问题解决。