0xbe5077ed
0xbe5077ed

Reputation: 4765

Can I create a bean from a SpEL expression directly?

I have a SpEL expression that generates a SpEL list. (The result type is actually a Java array, but no sense quibbling.)

I want "promote" the result of my SpEL expression to a bean in my Spring XML beans configuration file. One way to do this that works is to construct an ArrayList bean and supply the array as a constructor-arg like so:

<bean name="myList" class="java.util.ArrayList">
    <constructor-arg value="#{ my SpEL expression here }"/>
</bean>

This seems a bit cumbersome to me. Is there a one-liner that does the same thing and lets me put the array directly into a bean without having to wrap it in a newly-constructed ArrayList bean?

If not, is there a terser or cleaner syntax for doing what I'm doing now?

Upvotes: 2

Views: 662

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121427

Well, the same issue I have with the bean definition for just java.lang.String:

<bean id="myString" class="java.lang.String">
    <constructor-arg value="foo"/>
</bean>

And I live with that already for a long time. So simple to try to hack and don't do the real work :-).

How about to try this:

<util:list id="myList">
    <value>#{ my SpEL expression here }</value>
</util:list>

From other side you can just go to the JavaConfig and be free from this XML crutches. You should understand that XML definition is a bit restricted to the XML language boundaries and forgive him some inconveniences.

Upvotes: 1

Related Questions