本人自己搭建微服务项目,搭建公共模块, 发现的一个坑, 引入@configuratioPropertirs读取配置文件失效, 翻阅网络教程, 均不符
这里提出一个新的案例, 内容如下
@Component @Data @ConfigurationProperties(prefix = "jwts") public class JwtProperties { private String secret; private Integer expire; }
配置文件内容:
jwts: secret: qweqwe expire: 360000
到这里, 一直注入为空, 什么META-INF都尝试了, 仍然获取不到值, 本人于是索性把公共模块配置文件application.yml 改为aplication-common.yml, 然后在业务模块配置文件中, 引入
spring:
profiles:
active: common
这里贴上测试结果
@GetMapping("/test") public RetMsg<?> test() { System.out.println(ProjectJwtConfig.getJwt()); return RetMsg.success(jwtProperties.getSecret() + jwtProperties.getExpire()); }返回:
如上: