Spring boot YAML 配置:参数化键 [英] Spring boot YAML config : Parametred keys

查看:24
本文介绍了Spring boot YAML 配置:参数化键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is spring boot allow to use parametred keys using YAML ?

参数化键示例:

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

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

And in my Java class i want to do some thing like this:

@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

推荐答案

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

If your email and name are also configuration keys, you can refer to them in your message (but it's a bit weird)

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 以获取配置的类型安全绑定).

The Environment has no such API and as a Spring Boot user you shouldn't even touch that stuff (check @ConfigurationProperties for type safe binding of configuration).

这篇关于Spring boot YAML 配置:参数化键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆