如何在ChangeSpringPropertyKey OpenRewrite Recipe中正确使用通配符?
解决Spring配置中
sleuth节点替换为observation的动态路径问题 核心问题原因
之前的配置错误在于没有使用捕获分组语法保留动态节点(比如myconfig),直接用*或{*}无法让Rewrite识别需要保留的原始节点名称,导致中间节点被错误替换。
正确的ChangeSpringPropertyKey配置
YAML/Properties通用配置
用括号()捕获需要保留的动态部分,在newPropertyKey中用$1、$2等变量引用捕获内容:
- org.openrewrite.java.spring.ChangeSpringPropertyKey: oldPropertyKey: me.cxf.jaxws.services.(*).sleuth newPropertyKey: me.cxf.jaxws.services.$1.observation
效果验证
处理后原YAML会正确保留myconfig节点,完成sleuth到observation的替换:
me: cxf: jaxws: services: myconfig: retry: enabled: true observation: enabled: true
Properties格式场景示例
比如要将a.b.c.property=Something和a.f.c.otherProperty=Something修改为a.b.d.property=Something和a.f.d.otherProperty=Something,配置如下:
- org.openrewrite.java.spring.ChangeSpringPropertyKey: oldPropertyKey: a.(*).c.(*) newPropertyKey: a.$1.d.$2
这里(*)分别捕获b/f和property/otherProperty,$1、$2对应引用这些捕获值,实现动态路径替换。
关键说明
oldPropertyKey中的(*)是捕获分组,用于匹配并保留任意单个节点/属性段newPropertyKey中的$n(n为数字)对应oldPropertyKey中第n个(*)捕获的内容,顺序一一对应- 该配置同时支持YAML层级结构和Properties的点分隔键格式,无需分开处理
内容的提问来源于stack exchange,提问作者JGleason
相关产品推荐
相关产品推荐

