Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inject default values into Spring bean with annotation

I'd like to set default values for a list in a bean using annotations.

For example, if it's not a list you can do:

@Value("myValue")
String test;

But, in my case i want to give default values for a List of String.

List<String> tests;

In XML, it's something like that:

<bean id="beanId" class="class...">
    <property name="tests">
        <list>
            <value>value 1</value>
            <value>value 2</value>
            <value>value 3</value>
        </list>
    </property>
</bean>

I wanted to know if there is an existing annotation, or if I have to create one ?

Thank you

like image 298
RoD Avatar asked Nov 27 '25 20:11

RoD


1 Answers

@Value understands expression language, so you can use arbitrary method calls, although the syntax may get ugly, something like this:

@Value("#{T(java.util.Arrays).asList('Value 1','Value 2','Value 3')}")

Reference:

  • Spring Expression Language
  • Annotation based Configuration
like image 168
Sean Patrick Floyd Avatar answered Nov 30 '25 08:11

Sean Patrick Floyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!