代码之家  ›  专栏  ›  技术社区  ›  Lici

使用Spring属性占位符从文件.properties读取列表

  •  14
  • Lici  · 技术社区  · 16 年前

    上下文文件

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

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

    任何帮助都将不胜感激

    6 回复  |  直到 12 年前
        1
  •  13
  •   Marcin Cylke    14 年前

    util:properties element

    <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

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

        2
  •  10
  •   Paul Pacheco    13 年前

    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
  •  5
  •   badp    11 年前
    <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
  •  2
  •   Rakesh Juyal    16 年前

    我在这里看到的唯一方法是实现接口 MessageSourceAware

    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
  •  2
  •   Paul Whelan    16 年前

    <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
  •  -3
  •   Brad Larson    14 年前

    有几种方法,其中之一如下。

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