【发布时间】: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 配置文件?