简体   繁体   中英

spring bean from list<String> in a properties file

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM