如何按条件创建 spring 属性?

How to create spring properties by condition?

是否可以根据条件创建application.properties属性?

我想定义一个文件名,如果 spring.profiles.active=test 则应为 "default_name_test",否则为 "default_name"。

伪代码:

#application.properties:
filename=default_name${spring.profiles.active} = test ? "_test" : "";

这可能吗?

正如 Dave Syer 在他的评论中已经提到的,可以使用配置文件特定的 属性 文件通过 Spring Boot 配置应用程序。因此,在您的 application.properties 中,您将 属性 定义为其默认值,而在文件 application-test.properties 中,您将 属性 定义为 default_name_test。配置文件的名称模式是 application-${profilename}.properties.

在此处查看 Spring 引导手册:http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#boot-features-external-config-profile-specific-properties