【问题标题】:How to switch between properties file depending on selected profile如何根据所选配置文件在属性文件之间切换
【发布时间】:2018-11-06 16:08:26
【问题描述】:

我们的个人资料描述为:

<profiles>
     <!-- Local/Windows development  -->
    <profile>
        <id>local</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>localhost</INSTALL_MACHINE_LIST>
            <COPY_MODE>local</COPY_MODE>
        </properties>
    </profile>
    <!-- Development -->
    <profile>
        <id>dev</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dev01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- QA -->
    <profile>
        <id>qa</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dqa01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- Production -->
    <profile>
        <id>prod</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>prod01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
</profiles>

对于我们的测试环境,我们有 2 个属性文件 (在 src/main/test/resources 下)application-local.properties 和 application.properties 文件。计划是在“本地”配置文件模式(在我们的开发 Windows 系统上)中使用 application-local.properties,其余配置文件模式使用 application.properties。在 spring 上下文 (spring-context.xml) 中,目前,我们根据我们使用的配置文件在 2 个属性文件之间手动切换。寻找一种为“本地”配置文件自动选择 application-local.properties 和为任何其他类型的配置文件选择 application.properties 的方法。有没有办法在基于 xml 的 spring-context 文件中使用 if-then-else 条件? 我试过了:

<bean id="flag" class="java.lang.Boolean">
    <constructor-arg value="#{ profile == 'local' ? true: false }" />
</bean>
<util:properties id="machineMetaDbProps" location="#{ flag ? 'application-local.properties' : 'application.properties' }"/>

出现错误:

Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'profile' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?'

【问题讨论】:

  • 你说的是 Maven 配置文件还是 Spring 配置文件?

标签: java spring


【解决方案1】:

尝试像这样命名您的属性文件:

application.properties
application-prod.properties
application-test.properties

并在启动应用时使用“-Dspring.profiles.active=test”

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment

【讨论】:

  • 我只能使用 2 个属性文件:一个用于本地配置文件,另一个用于其余配置文件
  • 所以使用application-local.properties和application.properties
【解决方案2】:

Xml 配置:

在基于 XML 的配置中,您可以通过以下方式使 Spring 可以访问与配置文件相关的属性文件:

<beans profile="local">
    <context:property-placeholder 
             location="classpath:docker-db.properties" ignore-unresolvable="true"/>
</beans>
<beans profile="test">    
      <context:property-placeholder
             location="classpath:test-db.properties" ignore-unresolvable="true"/>
</beans>

Java 配置:

关于活动配置文件,您还可以手动将属性文件提供给 spring vi java 配置:

@Configuration
@Profile("local")
public class LocalPropertyReader {

    @Bean
    public static PropertyPlaceholderConfigurer properties() {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[] {
            new ClassPathResource("docker-db.properties"), new ClassPathResource("application-local.properties")
        };
        ppc.setLocations(resources);
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }
}



@Configuration
@Profile("test")
public class ProdPropertyReader {


    @Bean
    public static PropertyPlaceholderConfigurer properties() {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[] {
            new ClassPathResource("test-db.properties"), new ClassPathResource("application-test.properties")
        };
        ppc.setLocations(resources);
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }
}

启用配置文件:

这可以通过以下方式完成:

  • 使用 Spring 上下文环境:ctx.getEnvironment().setActiveProfiles("local");
  • 使用系统属性:System.setProperty("spring.profiles.active", "local");
  • 在运行时传递系统参数:-Dspring.profiles.active="local"
  • 在 web.xml 中启用配置文件

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>local</param-value>
    </context-param>
    

更多信息:

Load environment configurations and properties with Spring Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2023-03-21
    • 2017-05-06
    • 1970-01-01
    • 2012-05-27
    • 2022-12-25
    • 1970-01-01
    相关资源
    最近更新 更多