在弹簧中使用带有条件的@Value以将值映射到字符串

USe*_*299 5 java spring spring-mvc spring-properties

我有一个application.properites文件,其中包含以下内容:

xxx.xxx = sandbox
xxx.sandbox = 123
xxx.production = 456
Run Code Online (Sandbox Code Playgroud)

我希望在xxx.xxx == sandbox的情况下映射到字符串值123,在xxx.xxx == production的情况下映射到456

...
public class temp { 

 @Value("${??????}")
    private String token;
}
Run Code Online (Sandbox Code Playgroud)

是否有可能填补一个被煽动的条件?根据xxx.xxx将令牌映射到123或456?

USe*_*299 9

一个简单的方法,以防有人会遇到这个问题:

@Value("#{'${xxx.xxx}'=='sandbox' ? '${xxx.sandbox}' : '${xxx.production}'}")
Run Code Online (Sandbox Code Playgroud)

我只是觉得开始使用配置文件要容易得多.