带有MethodInvokingFactoryBean的PropertySourcesPlaceholderConfigurer - 未解析的值

时间:2015-01-14 18:39:43

标签: properties spring-3 unresolved-external

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

<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}&amp; $ {key2}未解决。我期待这些值将被解析,因为我使用PropertySourcesPlaceholderConfigurer加载datafile.properties。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

  1. 档案datafile.properties
  2. key1=Hellow
    key2=world.
    
    1. 在root-content.xml中配置
    2. <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中:
      2. @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时,可能会导致异常:Could not resolve placeholder 'key1' in string value "${key1}"。每个使用过的placeholderConfigurer必须有一个不同的占位符。

        例如:

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