简体   繁体   中英

Spring: PropertyPlaceHolderConfigurer incrementing integer infix property name

I'm trying to use PropertyPlaceHolderConfigurer to read properties of this type:

server.0.name=aaa
server.0.port=9999
server.1.name=bbb
server.1.port=9998
...
server.n.name=serverName
server.n.port=serverPort

Is there any way to inject it as a list of servers? List<Server> servers; Where the Server is some bean.

The reason you can not do this is that you are trying to create new Server Instances. The PropertyPlaceHolderConfigurer is afaik not able to create beans. But you can set the property in the beans definition

<bean id="server" class="ParentClass">
    <property name="servers">
        <list>
            <bean class="Server">
                <property name="name" value="aaa"/>
                <property name="port" value="123"/>
            </bean>
            ...
            more beans
            ...
        </list>
    </property>
</bean>

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