Spring开机,使用配置文件加载文件

Spring boot, use profiles to load files

我已经使用 spring-boot profiles 更改了不同环境的 属性 值,现在我想使用相同的方法来加载不同的资源文件,例如我有 dev_queries 和 prod_queries.xml 有 sql 个查询。

如果活动配置文件是 dev,我如何使 spring-boot 加载 dev_queries.xml,否则 prod_queries.xml。我知道我可以检查活动配置文件,但我的想法是不添加处理这种情况的特定逻辑。

将文件名外部化为自定义 属性 (docs, especially 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 文件的代码中,您不必有条件地检查当前活动的配置文件,但可以将该设置外化到属性中。