带有 MethodInvokingFactoryBean 的 PropertySourcesPlaceholderConfigurer - 未解析的值

PropertySourcesPlaceholderConfigurer with MethodInvokingFactoryBean - unresolved value

提问人:mformusic 提问时间:1/15/2015 最后编辑:djvmformusic 更新时间:3/9/2015 访问量:1112

问:

我正在使用 PropertySourcesPlaceholderConfigurer 访问包含 2 个值的文件: key1=值 1 key2=value2。

<bean id="mainProperties"      
      class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" id="">
    <property name="locations">
        <list>
           <value>file:datafile.properties</value>
        </list>
    </property>
</bean>

然后,使用 MethodInvokingFactoryBean 将 datafile.properties 中的值设置为系统属性。

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
        <!-- System.getProperties() -->
        <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="targetClass" value="java.lang.System" />
            <property name="targetMethod" value="getProperties" />
        </bean>
    </property>
    <property name="targetMethod" 
              value="putAll" />
    <property name="arguments">
        <!-- The new Properties -->
        <util:properties>
            <prop key="my.key1">${key1}</prop>
            <prop key="my.key2">${key2}</prop>
        </util:properties>
    </property>
</bean>

问题 - ${key1} 和 ${key2} 未解决。我期望这些值将被解析,因为我正在使用 PropertySourcesPlaceholderConfigurer 加载 datafile.properties。有人可以帮忙吗?

属性 spring-3 unresolved-external

评论


答:

0赞 Frantisek Hlavan 3/7/2015 #1

我是春季的初学者。我的解决方案是:

  1. 文件:datafile.properties
key1=Hellow
key2=world.
  1. 根内容.xml 中的配置
<bean id="propertySources"
      class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
      <array>
        <value>/WEB-INF/jdbc.properties</value>
        <value>/WEB-INF/datafile.properties</value>
      </array>       
    </property>
</bean>

<bean id="sysProps"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="java.lang.System"/>
    <property name="targetMethod" value="getProperties"/>
</bean>

<bean id="myNewProps"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="sysProps"/>
    <property name="targetMethod" value="putAll"/>
    <property name="arguments">
      <map>
        <entry key="my.key1" value="${key1}"/>
        <entry key="my.key2" value="${key2}"/>
      </map>
    </property
</bean>
  1. 在我的中央控制器中:MainController.java
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
  //...
  Properties prop = System.getProperties();
  String myKey1 = prop.getProperty("my.key1");
  String myKey2 = prop.getProperty("my.key2");

  System.out.println("\nProperties:\n my.key1 = " + myKey1 +
  "\n my.key2 = " + myKey2);
  //...
}

控制台输出:

Properties:
 my.key1 = Hellow
 my.key2 = world.

注意:

使用两个 placeholderConfigurers 时,可能会导致异常: 。每个使用的 placeholderConfigurer 都必须具有不同的占位符。Could not resolve placeholder 'key1' in string value "${key1}"

例如:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/jdbc.properties"/>        
</bean>
<!-- default placeholder ${ } -->

<bean id="propertySources"
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
      <array>    
        <value>file:datafile.properties</value>
      </array>       
    </property>
    <property name="placeholderPrefix" value="#["></property>
    <property name="placeholderSuffix" value="]"></property>        
</bean> 
<!-- placeholder #[ ] for datafile.properties -->