可以在Spring的@Value注释中指定多个属性名吗?

AEv*_*ans 8 java spring properties spring-boot

我已经熟悉Spring @Value注释的基本行为,将字段设置为项目属性的值,如下所示:

项目的属性文件

foo.bar=value
Run Code Online (Sandbox Code Playgroud)

项目的配置类

@Configuration
public class MyConfig {
    @Value("${foo.bar}")
    private String myValue;
}
Run Code Online (Sandbox Code Playgroud)

但是,我正在尝试使用条件配置创建一个SpringBoot启动项目,并希望将属性名称标准化为有用的东西,例如"com.mycompany.propertygroup.propertyname",但为了简化转换并鼓励采用,我想支持一段时间内的旧属性名称,因此想知道是否有某种方法允许多个属性名称设置相同的字段?例如:

我的理论初学者的配置

@Configuration
public class MyConfig {
    @Value("${com.mycompany.propertygroup.propertyname}" || "${oldconvention.property}")
    private String myValue;
}
Run Code Online (Sandbox Code Playgroud)

项目A的财产

oldconvention.property=value
Run Code Online (Sandbox Code Playgroud)

项目B的财产

com.mycompany.propertygroup.propertyname=value
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到任何关于这是否可行的文件或SO答案以及如何实现它......所以我想知道是否可能,或者如果不可能,是否有替代方案@Value注释可以用来达到同样的效果?

编辑澄清: 我不想跟踪多个值,所以我并不需要如何让多个值指令......目标是合并成一个单一的值是可以有多个名称.在实践中,每个项目使用启动器只会有一个名称值...只有在极少数情况下,当有人忘记删除旧属性时才会使用每个属性名称(并且它可能具有相同的值) ).在这种情况下,新的公约名称价值只能是一次使用.

更新

虽然提供的SpEL表达式答案在两个属性都存在时都有效,但只有一个属性名称存在时才能加载应用程序上下文.例:

更新的配置类

@Value("#{'${com.mycompany.propertygroup.propertyname}' != null ? '${com.mycompany.propertygroup.propertyname}' : '${oldconvention.propertyname}'}"
private String myProperty;
Run Code Online (Sandbox Code Playgroud)

更新的属性文件

com.mycompany.propertygroup.propertyname=somevalue
Run Code Online (Sandbox Code Playgroud)

错误

Caused by: java.lang.IllegalArgumentException: 
Could not resolve placeholder 'oldconvention.propertyname' in value
"#{'${com.mycompany.propertygroup.propertyname}' != null ? '${com.mycompany.propertygroup.propertyname}' : '${oldconvention.propertyname}'}"
Run Code Online (Sandbox Code Playgroud)

要求两个属性名称都存在失败的目的,即允许实现项目使用旧约定或新约定配置此启动器...

另一个更新......

我一直在玩SpEL表达式,并且当属性存在时我已经进行了条件检查,但是当它没有时,我在事后解决了属性问题.我认为问题是因为属性默认值和复杂的SpEL表达式不能很好地结合在一起.

@Value("#{${com.mycompany.propertygroup.propertyname:null} != null ? '${com.mycompany.propertygroup.propertyname}' : '${oldconvention.propertyname}'}")
private String myProperty;
Run Code Online (Sandbox Code Playgroud)

当我的SpEL编写如上所述时,我得到一个无法解析属性占位符异常,这意味着必须存在两个属性才能评估SpEL表达式.所以我开始思考,我可以使用我见过的默认属性语法来解析可选属性:@Value("${myoptionalproperty:defaultValue}")

以下是我尝试将默认属性解析与SpEL表达式结合起来:

@Value("#{${com.mycompany.propertygroup.propertyname:null} != null ? '${com.mycompany.propertygroup.propertyname:}' : '${oldconvention.propertyname:}'}")
private String myProperty;
Run Code Online (Sandbox Code Playgroud)

使用默认属性表示法时,我不断收到此错误:

org.springframework.expression.spel.SpelParseException: 
EL1041E: After parsing a valid expression, there is still more data in the expression: 'colon(:)'
Run Code Online (Sandbox Code Playgroud)

当我用Google搜索该错误时,流行的答案是,属性必须用单引号括起来,以便它们评估为字符串...但它们已经被包裹(除了第一个......我必须打开那个,因为我想要评估null检查的文字null.所以我认为默认值在包含在拼写表达式中时不能与属性一起使用.事实上,当一个@Value注释只用一个纯属性持有者设置时,我只见过默认属性集,而我在SpEL表达式中看到的所有属性都没有默认设置.

Jus*_*ano 7

您可以使用以下@Value注释:

@Configuration
public class MyConfig {

    @Value("#{'${com.mycompany.propertygroup.propertyname:${oldconvention.propertyname:}}'}")
    private String myValue;
}
Run Code Online (Sandbox Code Playgroud)

如果提供了此@Value注释,则使用该注释com.mycompany.propertygroup.propertyname;默认情况下,oldconvention.property使用com.mycompany.propertygroup.propertyname不提供的注释。如果两者均未提供,则将该属性设置为null。您可以通过替换null为另一个所需的值来将此默认值设置为另一个值。

有关更多信息,请参见以下内容:


或者,您可以捕获两个值并在返回值之前进行选择:

@Configuration
public class MyConfig {

    @Value("${com.mycompany.propertygroup.propertyname:}")
    private String newValue;

    @Value("${oldconvention.propertyname:}")
    private String oldValue;

    public String getValue() {

        if (newValue != null && !newValue.isEmpty()) {
            // New value is provided
            System.out.println("New Value: " + newValue);
            return newValue;
        }
        else {
            // Default to the old value
            return oldValue;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)


pvp*_*ran 5

使用 SPEL 是解决此问题的最佳方法。这应该工作

@Value("#{'${com.mycompany.propertygroup.propertyname}' != null ? '${com.mycompany.propertygroup.propertyname}' : '${oldconvention.property}'}")
private String myValue;
Run Code Online (Sandbox Code Playgroud)