0

I have a file.properties like this:

parameterkey=one
parameterval=oneVal

parameterkey=two
parameterval=twoVal

parameterkey=three
parameterval=threeVal

How can I set the property bean to wire parameterkey string list and parameterval string list?

Now I have this, but it wires only the last parameter and value in the appropriate variable:

<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>
michele
  • 26,348
  • 30
  • 111
  • 168
  • Im pretty sure this can help you (especially the second answer)- http://stackoverflow.com/questions/12576156/reading-a-list-from-properties-file-and-load-with-spring-annotation-value – deeveeABC Apr 15 '16 at 10:50
  • Just make it a comma separated list `parameterkey=one,two-three`... Spring will do the conversion. – M. Deinum Apr 15 '16 at 10:50

1 Answers1

2

If you have this in the properties

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

you can have @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
}

See here for more details.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • I create a PropertiesConfiguration class, but @configurationproperties is unresolved. What may be wrong? Thanks – michele Apr 20 '16 at 09:53
  • `@ConfigurationProperties` are a Spring Boot feature. I strongly suggest using Spring Boot instead of plain Spring configuration. You can scaffold a spring boot project from http://start.spring.io/ or from Spring Tool Suite. – Evgeni Dimitrov Apr 20 '16 at 10:08