0

I have used spring-boot profiles to change property values for different environments, now I want to use the same approach to load different resource files, ie example I have dev_queries and prod_queries.xml with sql queries.

How can I make spring-boot load dev_queries.xml if active profile is dev and prod_queries.xml otherwise. I know that I can check the active profile but my idea is to do not add specific logic for handle this situation.

4

1 回答 1

3

将文件名外部化为自定义属性(docs,尤其是24.4)是否有帮助?因此,在您的属性中,您将使用:

# application-dev.properties
myapp.query-source=dev_queries.xml

# application-production.properties
myapp.query-source=prod_queries.xml

在您的应用程序 bean 中,可以使用@Value注释访问此设置:

@Value("${myapp.query-source}")
private String querySource;  // will be dev_queries.xml or prod_queries.xml

这样,在您加载 xml 文件的代码中,您不必有条件地检查当前活动的配置文件,但可以将该设置外部化到属性中。

于 2016-01-24T12:05:45.440 回答