Defining a list in a Spring application context file
Can you create a List in a Spring application-context.xml file without using the <list>
element?
I have a bean whose constructor takes a Collection
object and I want to pass the entire list through the "value" attribute. The reason is that this value comes from a .properties file and you can't define a list in a .prop开发者_开发问答erties file.
I want to do something like this...is it possible?
MyClass.java:
public class MyClass{
public MyClass(Collection<String> collection){ /* ... */ }
}
application-context.xml:
<bean name="myBean" class="com.company.MyClass">
<constructor-arg value="${the.value}" />
</bean>
.properties file:
the.value=item1,item2,item3
Thanks.
<bean name="myBean" class="com.company.MyClass">
<constructor-arg>
<bean class="org.springframework.util.StringUtils" factory-method="commaDelimitedListToSet">
<constructor-arg type="java.lang.String" value="${the.value}"/>
</bean>
</constructor-arg>
</bean>
A possible solution for your problem is to pass a single string to your constructor and then parse the list inside the constructor using String.split()
.
I don't believe so, but you could have a constructor that built the list from a string.
(Actually, not entirely sure I'm correct--you could probably play significant games with custom placeholder configurators, although whether or not you should is probably debatable :)
This works. You can use SPEL.
In the bean config,
<property name="collection" value="#{{'item1','item2','item3'}}"/>
精彩评论