问题描述: 在nacos配置中心中获取配置的属性时,我用 @Value("${pattern.dateformat}")
这样是有用的,但换成@ConfigurationProperties(prefix = "pattern")
和配置类一起使用时就和配置文件关联不起来了,大佬们能帮我看一下吗感谢
呃,还有可能会有人吐槽我cloud-user-api-dev的命名方式,虽然是挺那啥的,确实不是因为这个。。因为@Value那样写都生效
版本
springboot 2.3.7
springcloud 2.2.7
nacos 2.0
单独的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中注册的对象,确实是成员没和配置文件关联上
以下方法我尝试过,但还是没有解决
- 在启动类上加了
@EnableConfigurationProperties
注解 - 配置类上换成
@NacosConfigurationProperties(prefix = "pattern",dataId = "cloud-user-api.yaml")
但是这里似乎不让用了。。