java - 在 spring 中将 @Value 与条件一起使用,以便将值映射到字符串

标签 java spring spring-mvc spring-properties

我有一个包含以下内容的 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/

相关文章:

Java Slug 创建到 Python

Java xml解码列表嵌套对象问题

java - Spring MVC Web 应用程序检测暴力攻击的最佳方法?

java - Bean创建异常: Error creating bean with name 'pgService' defined in class path resource

java - 在JAVA中用字符 '{'定义字符串

java - 尝试返回数组之和,方法调用错误

spring - <mvc :default-servlet-handler/>有什么需要和使用

java - spring 错误 :org. springframework.web.context.ContextLoader - 上下文初始化失败

javascript - spring4 和 spring security 上的 websocket SockJS/info?t=

java - CannotLoadBeanClassException : Cannot find class [org. springframework.jdbc.datasource.DriverManagerDataSource]