java - 为什么 Spring 3.x 忽略 PropertyPlaceholderConfigurer 的某些占位符前缀?

标签 java spring

我有下面的 bean 定义。如果我将“exposeSystemProperties”bean 的 placeholderPrefix 更改为“${”并在第二个 bean 的属性路径中使用它,它就会起作用。如果我将其更改为“%{”以外的任何内容,它将不起作用。我不能使用任何其他字符串(例如“$sys{”、“#[”等)。我目前使用的是 3.0.5.RELEASE。

有什么想法吗?更重要的是,我有第三个 PropertyPlaceHolderConfigure,因此只有两个前缀是行不通的。

  <bean id="exposeSystemProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="placeholderPrefix"><value>$sys{</value></property>
    <property name="order" value="10" />
  </bean>

  <bean id="localFileProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
    <property name="placeholderPrefix" value="%{" />
    <property name="placeholderSuffix" value="}" />
    <property name="order" value="20" />
    <property name="locations">
      <array>
        <bean class="java.lang.String">
            <constructor-arg><value>classpath:properties/$sys{deploy.env}/client.properties</value></constructor-arg>
        </bean>
      </array>
    </property>
  </bean>

最佳答案

由于您需要前缀来控制特定于环境的属性,因此可以通过使用系统变量(而不是示例中的 deploy.env property 来完成此操作):

 <value>classpath:properties/${ENV_SYSTEM:dev}/client.properties</value>

在这种情况下,它总是会在下面查找:

 <value>classpath:properties/dev/client.properties</value>

默认情况下,除非设置了ENV_SYSTEM系统变量。例如,如果设置为“qa”,它将自动查找:

 <value>classpath:properties/qa/client.properties</value>

如果您愿意“展望 future ”,另一种方法是使用 Spring 3.1 的 PROFILE feature ,其中 beans 可以是特定于配置文件的。例如:

<beans profile="dev">
    <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
        <jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
    </jdbc:embedded-database>
</beans>

仅当配置文件设置为 dev 时才会加载此dataSource:

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();

关于java - 为什么 Spring 3.x 忽略 PropertyPlaceholderConfigurer 的某些占位符前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602690/

相关文章:

Spring Data Elasticsearch 需要名为 id 的属性

java - 运行并行作业时如何安全地将参数从 Tasklet 传递到步骤

java - 创建 applicationContext.xml 时出错 : Error creating bean with name 'sessionFactory' defined in ServletContext resource

java - 为什么最好从方法类的实例中静态调用静态方法?

java - hibernate 删除查询不起作用

java - 如何在不知道数组内容的特定子类型的情况下复制数组的内容?

java - Spring Boot Web无法将嵌套的json解析为对象

java - Tomcat + CDI + JSF2 但我的交易在哪里?

java - JScrollPane 不显示滚动条

spring - 将Guice组件集成到Spring应用程序中