如何根据所选配置文件在属性文件之间切换

时间:2018-11-06 16:08:26

标签: java spring

我们的个人资料描述为:

<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 < / strong>文件。计划是在“本地”配置文件模式(在我们的开发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?'

2 个答案:

答案 0 :(得分:3)

尝试这样命名属性文件:

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

答案 1 :(得分:1)

Xml配置:

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

<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 Config:

关于活动配置文件,您还可以通过以下方式将属性文件手动输入到spring vi java config中:

@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