Spring启动YAML配置:参数密钥

时间:2015-10-06 13:05:00

标签: java spring spring-boot yaml

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

1 个答案:

答案 0 :(得分:2)

如果您的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是否有类型安全绑定配置)。