我有一个包含以下内容的 application.properites 文件:
xxx.xxx = sandbox
xxx.sandbox = 123
xxx.production = 456
如果 xxx.xxx == sandbox,我想映射到字符串值 123,如果 xxx.xxx == production,我想映射到字符串值 123
...
public class temp {
@Value("${??????}")
private String token;
}
是否可以填写 ??????这将根据 xxx.xxx 将 token 映射到 123 或 456?
最佳答案
一个简单的方法,以防有人会遇到这个问题:
@Value("#{'${xxx.xxx}'=='sandbox' ? '${xxx.sandbox}' : '${xxx.production}'}")
我只是认为开始使用配置文件要容易得多。
关于java - 在 spring 中将 @Value 与条件一起使用,以便将值映射到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37143562/