nacos配置中心用@ConfigurationProperties关联文件注入为null?

问题描述: 在nacos配置中心中获取配置的属性时,我用 @Value("${pattern.dateformat}")这样是有用的,但换成@ConfigurationProperties(prefix = "pattern")和配置类一起使用时就和配置文件关联不起来了,大佬们能帮我看一下吗感谢

呃,还有可能会有人吐槽我cloud-user-api-dev的命名方式,虽然是挺那啥的,确实不是因为这个。。因为@Value那样写都生效
版本
springboot 2.3.7
springcloud 2.2.7
nacos 2.0

image.png
单独的bootstrap.yaml

spring:
  application:
    name: cloud-user-api
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        file-extension: yaml

出现问题的配置类写法

@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    private String dataformat;
}

报错
exception is java.lang.NullPointerException: pattern] with root cause
我看了ioc中注册的对象,确实是成员没和配置文件关联上
image.png

以下方法我尝试过,但还是没有解决

  1. 在启动类上加了@EnableConfigurationProperties注解
  2. 配置类上换成@NacosConfigurationProperties(prefix = "pattern",dataId = "cloud-user-api.yaml")但是这里似乎不让用了。。
    image.png
阅读 4.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进