问题背景
搭建SpringCloud项目的时候,用户验证中心模块引用了Redis缓存模块,需要将RedisService使用@Autowired注解注入UserService。
除开在RedisService里添加@Component注解之外,还需要在resources下添加一个加载自动装配的类的文件。
照葫芦画瓢,springboot2.7之后,推荐使用org.springframework.boot.autoconfigure.autoconfiguration.imports。
在resources下添加META-INF文件夹,再在META-INF下添加spring文件夹,再添加org.springframework.boot.autoconfigure.autoconfiguration.imports文件
按需要添加之后,启动用户验证中心,控制台报错如下:
Description:
Field redisService in UserServiceImpl required a bean of type 'RedisService' 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 'RedisService' in your configuration.
问题分析
路径
该文件应在src/main/resources/META-INF/spring/下,查看是否写成了src/main/resources/META-INF.spring/。
没写错,pass。
核对文件内容
类的绝对路径是否一样。
一样,pass。
查看target
META-INF及下面的文件是否打包进去。
没有。
查看redis缓存模块的pom文件,发现多复制了一个
<packaging>pom</packaging>
wh柒八九
-maven的<packaging>pom</packaging>如何使用
问题解决
删掉redis缓存模块pom文件中的<packaging>pom</packaging>
mvn clean
重启用户验证中心。
成功。