2016-04-15 113 views
0

我有一个file.properties这样的:春天Bean文件

parameterkey=one 
parameterval=oneVal 

parameterkey=two 
parameterval=twoVal 

parameterkey=three 
parameterval=threeVal 

如何设置该属性的bean线parameterkey字符串列表和parameterval字符串列表?

现在我有这个,但它仅电线在适当的变量最后一个参数和值:

<context:property-placeholder location="${env}.properties"/> 
..... 
<spring:bean id="myBean" class="mygroup.MyClass"> 
      <spring:property name="queryParamKey"> 
       <spring:list value-type="java.lang.String"> 
        <spring:value>${parameterkey}</spring:value> 
       </spring:list> 
      </spring:property> 

      <spring:property name="queryParamVal"> 
       <spring:list value-type="java.lang.String"> 
         <spring:value>${parameterval}</spring:value> 
       </spring:list> 
      </spring:property> 
</spring:bean> 
+0

我很肯定这可以帮助你(特别是第二个答案) - http://stackoverflow.com/questions/12576156/reading-a-list-from-properties-file-and-load-with-spring-annotation值 – deeveeABC

+0

只要使它成逗号分隔列表'parameterkey = one,two-three' ... Spring将执行转换。 –

回答

2

如果您在属性

app.myType[0].key=key1 
app.myType[0].value=val1 
app.myType[1].key=key2 
app.myType[1].value=val2 

你可以有@ConfigurationProperties有这样的:

@ConfigurationProperties(prefix="app") 
@Component 
public class PropertiesConfiguration { 
    private List<MyType> myType; 

    public static class MyType { 
     private String key; 
     private String value; 

     //getters setters 
    } 
    //getters setters 
} 

查看here了解更多详情。

+0

我创建了一个PropertiesConfiguration类,但@configurationproperties未解析。什么可能是错的?谢谢 – michele

+0

'@ ConfigurationProperties'是一个Spring Boot功能。我强烈建议使用Spring Boot而不是普通的Spring配置。您可以从http://start.spring.io/或从Spring Tool Suite脚手架弹簧引导项目。 –