alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.21k stars 12.83k forks source link

spirng 中nacosvalue注解内进行split获取list结果异常 #4027

Closed FengJinghHua closed 3 years ago

FengJinghHua commented 4 years ago

spring中@value注解可以进行string的split并赋值为list对象,如下: @Value("#{'${support.type}'.split(',')}") Set supportType;

这个在@nacosValue中应该如何写才能得到对应效果?please help 我试过 @NacosValue(value = "${support.type:,#}split(',')}", autoRefreshed = true) 但是取到的值是["","#}split(", "')}"] 而不是 ["*", "#"] please help,thanks

KomachiSion commented 4 years ago

nacosValue应该不支持这个操作。

FengJinghHua commented 4 years ago

@KomachiSion ok~ 我试过在赋值是给了string类型,然后再指定一个list类型,如下:

@NacosValue(value = "${support.type:,#)}", autoRefreshed = true) String type1; private List familyList = Arrays.asList(type1.split(","));

不幸的是,启动的时候会报空指针异常,是和nacos启动时机有关系吗,或者说,如果要实现类似@value split 的功能,有workaround吗

KomachiSion commented 4 years ago

应该和启动顺序有关。

是否实现类似的功能需要看社区的反馈,看看是否社区中有人愿意贡献,如果社区里需求的人数比较多,我们也会提高这个功能的优先级。

horizonzy commented 4 years ago

@KomachiSion ok~ 我试过在赋值是给了string类型,然后再指定一个list类型,如下:

@NacosValue(value = "${support.type:,#)}", autoRefreshed = true) String type1; private List familyList = Arrays.asList(type1.split(","));

不幸的是,启动的时候会报空指针异常,是和nacos启动时机有关系吗,或者说,如果要实现类似@value split 的功能,有workaround吗

这个我来搞搞,和spring一样支持el表达式是吗。

FengJinghHua commented 4 years ago

@horizonzy 是的,这样的使用应该很普遍

horizonzy commented 4 years ago

这个PR. https://github.com/nacos-group/nacos-spring-project/pull/237

horizonzy commented 3 years ago

@horizonzy 是的,这样的使用应该很普遍

Hi, 最新的代码已经被合到nacos-spring-project,如果想要使用这个特性的话,可以单独对nacos-spring-project打包使用