Enter default values ​​in Spring bean with annotation

I would like to set the default values ​​for a list in a bean using annotations.

For example, if this is not a list you can do:

@Value("myValue")
String test;

But in my case, I want to give default values ​​for a list of strings.

List<String> tests;

In XML, this is something like this:

<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 an existing annotation exists, or do I need to create it?

thank

+3
source share
2 answers

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

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

Reference:

+7
source

You can directly assign default values ​​without annotations:

String test = "myValue";

List<String> tests = Arrays.asList("value 1", "value 2", "value 3");

@Value , , Spring , XML, , SpEL, .. , .

+4

Source: https://habr.com/ru/post/1785475/


All Articles