Java Springbean中属性的多个值
我需要在Springbean属性值中设置两个同名的值 目前我有 我在Java类中使用这个属性名:私有字符串siteUid强> 我的要求是添加另一个具有不同值的属性名称Java Springbean中属性的多个值,java,spring,Java,Spring,我需要在Springbean属性值中设置两个同名的值 目前我有 我在Java类中使用这个属性名:私有字符串siteUid 我的要求是添加另一个具有不同值的属性名称 请建议,如果我可以编写这两个属性值,如何在Java类中使用相同的属性值,您可以使用@Value注释 如果您的属性文件包含: foo.bar.property1=hello foo.bar.property2=world 您可以在Java类中使用: @Component public class SomeClass { @
请建议,如果我可以编写这两个属性值,如何在Java类中使用相同的属性值,您可以使用
@Value
注释
如果您的属性文件包含:
foo.bar.property1=hello
foo.bar.property2=world
您可以在Java类中使用:
@Component
public class SomeClass {
@Value("${foo.bar.property1}")
private String variable1; // will be set to "hello"
@Value("${foo.bar.property2}")
private String variable2; // will be set to "world"
}
请注意,实际变量的名称(即
variable1
和variable2
)不相关;它们可以是你想要的任何东西。重要的一点是@Value
中包含的字符串与属性文件中的键匹配。不太清楚添加是什么意思。一般来说,只要您要使用@Value
注释,到目前为止最灵活的方法就是在autowired构造函数中使用值:
@Component
public class MyComponent {
private String myProperty;
public MyComponent(@Value('${app.value1}') String value1, @Value('${app.value2}') String value2) {
// here you can even concatenate the value (hence I've said that its not clear what is 'add' but in general you can manipulate however you want
this.myProperty = value1 + value2;
}
}
您可以在这里使用Spring表达式语言添加/concat两个属性
@Value(“${property1}.concat(“${property2}”)字符串参数
这里的
concat
是String
方法。由于我对需求不是很清楚,所以您可以尝试使用其他字符串方法来实现您想要的。嗯,SpringBean属性总是引用相应Java类的字段,并且一个类中不能有两个同名字段。这是否回答了您的问题?是的,我犯了个愚蠢的错误。我来编辑