如何按条件创建弹簧属性?

mem*_*und 6 java spring spring-boot

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

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

伪代码:

#application.properties:
filename=default_name${spring.profiles.active} = test ? "_test" : "";
Run Code Online (Sandbox Code Playgroud)

这可能吗?

dun*_*nni 6

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

请参阅此处的 Spring Boot 手册:http : //docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#boot-features-external-config-profile-specific-properties