nacos中的配置使用@Value注解获取不到值的原因及解决方案
作者:渝小白 时间:2023-11-29 13:43:00
可能导致问题的原因:
1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-“激活的环境配置”."文件后缀”,
例如:nacosdemo-dev.yaml。
2.配置文件的后缀名写错了,后缀名有两种,yaml和properties,不能写成yml,项目中的配置文件中也要检查后缀名是否一致,如果用yaml类型的配置,需要在bootstrap.properties文件中添加这行配置来指定类型**“spring.cloud.nacos.config.file-extension=yaml**”
官网地址https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config
3.如果要使用nacos做为外部配置,就需要将连接nacos的配置写在bootstrap.properties或者bootstrap.yaml文件中,application.properties文件可以删除掉,或者application.properties中只配置server.port,注意:spring.application.name要和nacos中的配置文件名一致,不需要加后缀,如果是nacos中是yaml格式的配置加上spring.cloud.nacos.config.file-extension=yaml,不加默认是properties格式。
4.可能识别不到bootstrap.properties配置文件,加入以下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
完整依赖如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
来源:https://blog.csdn.net/qq_42425515/article/details/129746707
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis通用Mapper和PageHelper的过程详解
Java分析讲解序列化与字典功能的序列化
详解JAVA中priorityqueue的具体使用
![](https://img.aspxhome.com/file/2023/2/68222_0s.png)
android耳机左右声道接反具体修正方法
C#实现文件断点续传下载的方法
Java画笔的简单实用方法
Java 处理超大数类型之BigInteger案例详解
springboot配置文件中使用${}注入值的两种方式小结
![](https://img.aspxhome.com/file/2023/4/100064_0s.png)
JVM分配和回收堆外内存的方式与注意点
![](https://img.aspxhome.com/file/2023/1/129961_0s.jpg)
C# 通过 oledb 操作Excel实例代码
详解用Eclipse如何创建Web项目
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)
Android开发之开关按钮用法示例
![](https://img.aspxhome.com/file/2023/2/138882_0s.gif)
退出Android程序时清除所有activity的实现方法
Java跨域问题的处理详解
Android 消息机制以及handler的内存泄露
Java动态脚本Groovy获取Bean技巧
![](https://img.aspxhome.com/file/2023/5/132015_0s.png)
解决java执行cmd命令调用ffmpeg报错Concat error - No such filter '[0,0]'问题
![](https://img.aspxhome.com/file/2023/4/101924_0s.png)
C# 泛型集合的自定义类型排序的实现
![](https://img.aspxhome.com/file/2023/3/87033_0s.png)
Mybatis order by 动态传参出现的问题及解决方法
![](https://img.aspxhome.com/file/2023/3/64373_0s.png)
Android应用中仿今日头条App制作ViewPager指示器
![](https://img.aspxhome.com/file/2023/7/126747_0s.jpg)