有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring属性占位符配置需要在运行时动态替换

我在春天有一个属性类持有者配置

<context:property-placeholder location="classpath:${appConfig}" />

在我的类路径dev.properties和qa中有三个配置。属性和产品属性

我需要传递appConfig,它应该被替换为我在运行时动态传递的值

当我做这个java-jar应用程序时。jar-DappConfig=dev.properties将appConfig替换为dev.properties失败


共 (1) 个答案

  1. # 1 楼答案

    您可以使用${spring.profiles.active}并相应地命名属性文件以访问它们,例如:

    <context:property-placeholder location="classpath:${spring.profiles.active}.properties" />
    

    通过这种方式,您可以在类路径中存在dev.propertiesstage.properties等,并根据配置的概要文件选择适当的文件

    更新

    运行应用程序时,可以将配置文件值作为命令行参数传递,例如java -Dspring.profiles.active=dev -jar application.jar