Home > Back-end >  Spring 5 : read properties fragment (Not Spring Boot)
Spring 5 : read properties fragment (Not Spring Boot)

Time:02-19

Is it possible to read fragment of an array by @Value (spring 5) ?

Something like this :

INPUT data.properties :

list.numbers=1,2,3

READ

@Value("${list.numbers[0]}")
int firstNumber;

OUTPUT

firstNumber=1

CodePudding user response:

You can split and get the first element.

@Value("#{'${list.numbers}'.split(',')[0]}")
int firstNumber;

CodePudding user response:

A long time ago, I have used bean injection with XML configuration. You can read that blog article and using spring-core, spring-beans and spring-context-support dependencies.

Below you'll find the XML config of that bean injection with the <bean ref="beanListItemId"> (here : song1, song2, etc.) inside your bean list (here the list container is album):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="song1" >
        <property name="title" value="I Saw Her Standing There" />
        <property name="writer" value="Beatles" />
    </bean>

    <bean id="song2" >
        <property name="title" value="Misery" />
        <property name="writer" value="Beatles" />
    </bean>

    <bean id="song3" >
        <property name="title" value="Anna (Go to Him)" />
        <property name="writer" value="Beatles" />
    </bean>


    <bean id="album" >
        <property name="title" value="Please Please Me" />
        <property name="year" value="1963" />
        <property name="songs">
            <list>
                <ref bean="song1" />
                <ref bean="song2" />
                <ref bean="song3" />
            </list>
        </property>
    </bean>

</beans>
  • Related