我的 color.properties 文件为
rose = red
lily = white
jasmine = pink
我需要获取颜色值
String flower = runTimeFlower;
@Value("${flower}) String colour;
我们将在运行时获得花朵值。我怎样才能在java Spring中做到这一点?我需要在运行时根据用户输入获取单个值(从属性文件中定义的 50 个值中)。如果我不能使用 @Value ,你能告诉我其他方法来处理这个问题吗?
无法使用 @Value 执行您所描述的操作,但您可以这样做,这几乎是同一件事:
package com.acme.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class Example {
private @Autowired Environment environment;
public String getFlowerColor(String runTimeFlower) {
return environment.resolvePlaceholders("${" + runTimeFlower + "}");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)