FEATURED · 精选文章

No spring.config.import property has been defined 的解决办法

发布时间 / 2026/8/13 14:14:23
来源 / 创域科博编辑部
栏目 / 资讯中心
No spring.config.import property has been defined 的解决办法 一、问题概述在基于Spring Boot整合Nacos作为服务注册与发现和配置中心的开发过程中博主遇到的这个配置问题导致项目启动报错问题项目直接退出、无法正常启动“No spring.config.import property has been defined”基础环境说明本问描述为博主所使用的环境实际中根据个人项目系统版本进行调整本文配置和解决方案均基于以下技术栈版本也是当前主流的生产环境适配版本JDK版本17Spring Boot版本2.7.12Spring Cloud版本2021.0.5.0Spring Cloud Alibaba版本2021.0.5.0与Spring Cloud 2021.0.5.0配套Nacos版本2021.0.5.0适配上述Spring Cloud Alibaba版本问题现象启动整合了Nacos配置中心的Spring Boot项目时控制台抛出核心错误提示org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85) at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343) at org.springframework.boot.SpringApplication.run(SpringApplication.java:301) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) at com.huazai.speech.NacosConfigApplication.main(NacosConfigApplication.java:15) 13:59:42.579 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: No spring.config.import property has been defined Action: Add a spring.config.importnacos: property to your configuration. If configuration is not required add spring.config.importoptional:nacos: instead. To disable this check, set spring.cloud.nacos.config.import-check.enabledfalse. Disconnected from the target VM, address: 127.0.0.1:57874, transport: socket Process finished with exit code 1最终项目进程以 exit code 1 退出无法正常启动。入下图三、解决办法问题分析该错误的出现主要有两个关键原因且二者常同时存在配置机制变更Spring Cloud Alibaba 2021.0.5.0适配Spring Boot 2.7.x版本开始Nacos配置中心不再自动导入配置源框架要求开发者 显式声明是否要导入Nacos配置 未声明则触发校验错误如下图Bootstrap配置失效Spring Boot 2.4 版本默认移除了 bootstrap.yml/application.yml 的自动加载能力官方明确变更若未引入专属依赖即使配置了Nacos相关参数也会因配置文件未加载而触发报错。如下图配置机制变更解决办法基础配置必须读取Nacos配置否则启动失败如果你的项目 必须依赖Nacos中的配置才能正常运行 生产环境常用需要在配置文件中显式声明导入Nacos配置源并补充Nacos基础连接信息。配置示例YAML格式推荐修改 application.yml 或 bootstrap.yml 优先级更高spring: # 1. 核心配置声明导入Nacos配置源 config: import: optional:127.0.0.1:8848 # 2. Nacos连接基础配置 cloud: nacos: config: # Nacos服务器地址单机版 server-addr: 127.0.0.1:8848 # 命名空间可选默认public namespace: your-namespace-id # 配置组可选默认DEFAULT_GROUP group: DEFAULT_GROUP # 配置文件后缀可选默认properties file-extension: yml # 注册中心配置如果同时用Nacos做注册中心 discovery: server-addr: 127.0.0.1:8848 namespace: your-namespace-id配置示例Properties格式修改 application.properties #1. 核心配置声明导入Nacos配置源 spring.config.importoptional:127.0.0.1:8848 #2. Nacos连接基础配置 spring.cloud.nacos.config.server-addr127.0.0.1:8848 spring.cloud.nacos.config.namespaceyour-namespace-id spring.cloud.nacos.config.groupDEFAULT_GROUP spring.cloud.nacos.config.file-extensionyml #注册中心配置可选 spring.cloud.nacos.discovery.server-addr127.0.0.1:8848 spring.cloud.nacos.discovery.namespaceyour-namespace-idBootstrap配置失效解决办法在项目的 pom.xml 中添加 spring-cloud-starter-bootstrap 依赖版本3.1.3适配Spring Boot 2.7.12 Spring Cloud 2021.0.5.0恢复Bootstrap配置文件的加载能力!-- 恢复Spring Boot 2.7.12中被移除的bootstrap配置加载能力适配Spring Cloud 2021.0.5.0-- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-bootstrap/artifactId version3.1.3/version /dependency版本说明目前 3.1.3 是适配Spring Boot 2.7.12 Spring Cloud 2021.0.5.0的最优版本无需调整直接使用即可验证步骤完成依赖引入和配置修改后执行以下操作验证是否解决问题1、刷新Maven依赖点击IDEA右侧Maven面板的“Reload All Maven Projects”确保 spring-cloud-starter-bootstrap 已成功引入2、重启 Spring Boot项目3、若控制台无该错误提示且日志中能看到“Nacos Config properties”相关加载信息如 Nacos config server address : localhost:8848 则说明问题已解决。如下图补充说明1、版本强适配本文配置基于JDK 17 Spring Boot 2.7.12 Spring Cloud 2021.0.5.0若修改任一核心版本需同步调整 spring-cloud-starter-bootstrap 版本如Spring Boot 2.7.0对应3.1.02、配置文件优先级 bootstrap.yml 加载早于 application.yml Nacos核心配置建议放在 bootstrap.yml 中避免配置加载顺序问题3、命名空间配置Nacos的“命名空间”需填写ID非名称在Nacos控制台“命名空间”页面可复制对应ID否则会加载不到对应配置4、依赖冲突若项目中存在多个版本的 spring-cloud-starter-bootstrap 需在Maven依赖中排除低版本统一使用3.1.3版本。参考文档【Spring Cloud官方文档】【Spring Boot 2.4 Release Notes】好了关于 No spring.config.import property has been defined 的解决办法 就写到这儿了如果还有什么疑问或遇到什么问题欢迎扫码提问也可以给我留言哦我会一一详细的解答的。歇后语“ 共同学习共同进步 ”也希望大家多多关注CSND的IT社区。作 者华 仔联系作者who.seek.mejava98k.vip来 源CSDN (Chinese Software Developer Network)原 文https://blog.csdn.net/Hello_World_QWP/article/details/158039446版权声明本文为博主原创文章请在转载时务必注明博文出处
RELATED — 相关阅读

相关资讯

LATEST — 最新资讯

最新发布

TODAY — 本日精选

新闻

WEEKLY — 本周精选

新闻

MONTHLY — 本月精选

新闻