【问题标题】:Reading a list from a file .properties using Spring properties place holder使用 Spring 属性占位符从文件 .properties 中读取列表
【发布时间】:2010-12-08 14:49:06
【问题描述】:

我想使用 Spring 属性占位符填充 bean 列表属性。

上下文文件

<bean name="XXX" class="XX.YY.Z">
      <property name="urlList">
            <value>${prop.list}</value>
      </property>
</bean>

属性文件

prop.list.one=foo
prop.list.two=bar

任何帮助将不胜感激

【问题讨论】:

    标签: spring properties


    【解决方案1】:

    使用util:properties element 加载您的属性。您可以使用 PropertyPlaceholderConfigurer 指定文件的路径:

    <bean name="XXX" class="XX.YY.Z">
      <property name="urlList">
        <util:properties location="${path.to.properties.file}"/>
      </property>
    </bean>
    

    更新我误解了这个问题;您只想返回键以特定字符串开头的属性。实现这一目标的最简单方法是在 bean 的 setter 方法中执行此操作。您必须将字符串作为单独的属性传递给您的 bean。扩展上述声明:

    <bean name="XXX" class="XX.YY.Z" init-method="init">
      <property name="propertiesHolder">
         <!-- not sure if location has to be customizable here; set it directly if needed -->
        <util:properties location="${path.to.properties.file}"/>
      </property>
      <property name="propertyFilter" value="${property.filter}" />
    </bean>
    

    在你的XX.YY.Z bean 中:

    private String propertyFilter;
    private Properties propertiesHolder;
    private List<String> urlList;
    
    // add setter methods for propertyFilter / propertiesHolder
    
    // initialization callback
    public void init() {
      urlList = new ArrayList<String>();
      for (Enumeration en = this.propertiesHolder.keys(); en.hasMoreElements(); ) {
        String key = (String) en.nextElement();
        if (key.startsWith(this.propertyFilter + ".") { // or whatever condition you want to check
          this.urlList.add(this.propertiesHolder.getProperty(key));
        }
      } // for
    }
    

    如果您需要在许多不同的地方执行此操作,您可以将上述功能包装到 FactoryBean 中。

    【讨论】:

    • 我不认为这是他要问的,他只想注入属性文件的值,但只注入具有特定前缀键的值,并且他希望它们为 List .一组相当深奥和具体的要求。
    • 你是对的!我想使用前缀过滤作为列表加载的属性
    • @skaffman - 谢谢,你完全正确。我想以“prop.list”开头的属性应该是一个线索,我从来没有想过OP希望它们被过滤。
    【解决方案2】:

    一个更简单的解决方案:

    class Z {
        private List<String> urlList;
        // add setters and getters
    }
    

    你的 bean 定义

    <bean name="XXX" class="XX.YY.Z">
          <property name="urlList" value="#{'${prop.list}'.split(',')}"/>
    </bean>
    

    然后在你的属性文件中:

    prop.list=a,b,c,d
    

    【讨论】:

      【解决方案3】:
      <bean id="cpaContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
          <property name="urls">
          <bean class="org.springframework.util.CollectionUtils" factory-method="arrayToList">
              <constructor-arg type="java.lang.Object">
                  <bean class="org.springframework.util.StringUtils" factory-method="tokenizeToStringArray">
                      <constructor-arg type="java.lang.String" value="${myList}"/>
                      <constructor-arg type="java.lang.String" value=" "/>
                  </bean>
              </constructor-arg>
          </bean>
          </property>
      

      地点:

      myList=http://aaa http://bbb http://ccc
      

      【讨论】:

        【解决方案4】:

        我在这里看到的唯一方法是,实现接口“MessageSourceAware”以获取 messageResource,然后手动填充您的列表。

        class MyMessageSourceAwareClass implemets MessageSourceAware{
            public static MessageSource messageSource = null;
        
            public void setMessageSource(MessageSource _messageSource) {
                messageSource = _messageSource;
            }
        
            public static String getMessage( String code){
                return messageSource.getMessage(code, null, null );
            }
        
        }
        

        --- 属性文件 ---

        prop.list=foo;bar;one more
        

        像这样填充您的列表

        String strlist = MyMessageSourceAwareClass.getMessage ( "prop.list" );
        
        if ( StringUtilities.isNotEmptyString ( strlist ) ){
           String[] arrStr = strList.split(";");
           myBean.setList ( Arrays.asList ( arrStr ) );
        }
        

        【讨论】:

          【解决方案5】:

          只需添加以下 Bean 定义

          <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
              <list>
                  <value>classpath:myprops.properties</value>
              </list>
              </property>
          </bean>
          

          要像这样使用它,请注意端口是在 myprops.properties 中定义的

          <bean id="mybean" class="com.mycompany.Class" init-method="start">
              <property name="portNumber" value="${port}"/>
          </bean>
          
          【解决方案6】:

          有几种方法,下面是其中一种。

          XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
          PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
          cfg.setLocation(new FileSystemResource("jdbc.properties"));
          cfg.postProcessBeanFactory(factory);
          

          【讨论】:

            猜你喜欢
            • 2012-12-19
            • 1970-01-01
            • 2011-07-12
            • 1970-01-01
            • 2020-07-01
            • 2017-01-12
            • 1970-01-01
            • 1970-01-01
            • 2014-07-16
            相关资源
            最近更新 更多