【问题标题】:Spring boot YAML config : Parametred keysSpring boot YAML 配置:参数键
【发布时间】:2015-10-06 23:02:44
【问题描述】:

spring boot 是否允许使用 YAML 的参数化键?

参数键示例:

myapp.configured.key: This is your email > {0} And this is your name > {1}

在我的 Java 类中,我想做这样的事情:

@Inject
private Environment env;//import org.springframework.core.env.Environment;


String email = "email@email.com"
String name = "User Name";

String key=env.getProperty("myapp.configured.key", email, name);

System.out.println(key);

输出会是这样的:

This is your email > email@email.com And this is your name > User Name

【问题讨论】:

  • 如果你能做到String key=String.format(env.getProperty("myapp.configured.key"), email, name);,你为什么要这样做(假设你可以将占位符更改为%s
  • 因为在 Spring 中使用 MessageSource(org.springframework.context.MessageSource) 我们可以做一些类似上面的事情(比较)。但似乎很好的替代品;)。谢谢

标签: java spring spring-boot yaml


【解决方案1】:

如果你的emailname也是配置键,你可以在你的消息中引用它们(不过有点奇怪)

myapp.configured.email: email@example.com
myapp.configured.name: John Smith
myapp.configured.key: This is your email > ${myapp.configured.email} And this is your name > ${myapp.configured.name}

Environment 没有这样的 API,作为 Spring Boot 用户,您甚至不应该接触那些东西(检查 @ConfigurationProperties 以了解配置的类型安全绑定)。

【讨论】:

  • 嗨@Stéphane Nicoll,电子邮件和姓名不包含在我的文件配置中:(。我应该从我的控制器中获取它们并在之后设置它们
  • 那么你应该存储格式化的字符串并自己进行格式化。
猜你喜欢
  • 1970-01-01
  • 2019-10-15
  • 2015-05-31
  • 1970-01-01
  • 2016-01-17
  • 2020-05-07
  • 2019-10-21
  • 1970-01-01
  • 2021-06-03
相关资源
最近更新 更多