报错
报错信息如下
Description:
No spring.config.import property has been defined
Spring 官方给出的解决方案如下
Add a spring.config.import=nacos: property to your configuration.
If configuration is not required add spring.config.import=optional:nacos: instead.
To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.
出现原因
首先,自2021.0.5版本的 Spring Cloud 默认不再启用 bootstrap 包,因此应该将配置文件写在 application.yml 中,或手动在 maven 中导入 bootstrap 包。
解决方法
由于新版nacos要求我们在spring config import下配置新的nacos规则,我们仅需按照以下配置即可,按照nacos之前的的版本格式prefix−{prefix}-prefix−{spring.profiles.active}.${file-extension}
spring:
config:
import:
- optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}-`` `
# 其他
如果进行修改后,还是注入为空,请检查nacos和本地的文件名称是否一致,是否在@Value注解是否采用正确格式`@Value("${pattern.dateformat}")`.
本文介绍了在SpringCloud2021.0.5版本后,使用Nacos配置时遇到的Nospring.config.importpropertyhasbeendefined报错。解决方法包括将配置文件移至application.yml,添加spring.config.import=nacos配置,并确保文件名和@Value注解格式的一致性。
9064

被折叠的 条评论
为什么被折叠?



