Hello, is it possible to declare a List attribute (similar to <List><value>...</value></list> in an xml configuration) as a Properties configuration ?
Thanks a lot.
Sincerely,
John
Hello, is it possible to declare a List attribute (similar to <List><value>...</value></list> in an xml configuration) as a Properties configuration ?
Thanks a lot.
Sincerely,
John
Hi,
Following there is an example of declaring list in the spring container.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:list id="phones">
<value>12121</value>
<value>12122</value>
<value>3332423</value>
</util:list>
</beans>
I hope that will be helpful for you.
Regards
Actually, what I need is to be able to declare a list as a property and not as an xml.
The spring documentation has these examples
employee.(class)=MyClass // bean is of class MyClass
employee.(abstract)=true // this bean can't be instantiated directly
employee.group=Insurance // real property
employee.usesDialUp=false // real property (potentially overridden)
salesrep.(parent)=employee // derives from "employee" bean definition
salesrep.(lazy-init)=true // lazily initialize this singleton bean
salesrep.manager(ref)=tony // reference to another bean
salesrep.department=Sales // real property
techie.(parent)=employee // derives from "employee" bean definition
techie.(scope)=prototype // bean is a prototype (not a shared instance)
techie.manager(ref)=jeff // reference to another bean
techie.department=Engineering // real property
techie.usesDialUp=true // real property (overriding parent value)
ceo.$0(ref)=secretary // inject 'secretary' bean as 0th constructor arg
ceo.$1=1000000 // inject value '1000000' at 1st constructor arg
Is it possible to have a special syntax for a list attribute ?
techie.(list)=value ?
Something like that ?
Check this.
It seems that it was requested before but was not done.
https://jira.springsource.org/browse...3Aall-tabpanel
Thanks for the reply.
So I guess I can't expect it to be done sometime from now, huh ?
Any workarrounds you see that I could do ?
I really need this feature. :-(
Thanks again.
Sincerely,
John
Hi,
You can solve your problem using customEditors.
Following there is an example
In this example I am using the ListTypeEditor in order to set properly the attributes of type List.Code:public final class ClassWithListTesting { public void testSettingListAttribute() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.registerCustomEditor(List.class, ListTypeEditor.class); Properties p = new Properties(); p.setProperty("x1.(class)", ClassWithListAttribute.class.getName()); p.setProperty("x1.list", "a,b,c"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); // Getting the bean reference ClassWithListAttribute classWithListAttribute = (ClassWithListAttribute) lbf.getBean("x1"); System.out.println("List:" + classWithListAttribute.getList()); } public static void main(String[] args) { ClassWithListTesting test = new ClassWithListTesting(); test.testSettingListAttribute(); } private static class ClassWithListAttribute { private List list; public List getList() { return list; } public void setList(List list) { this.list = list; } } public static class ListTypeEditor extends PropertyEditorSupport { public void setAsText(String text) { String[] str = StringUtils.commaDelimitedListToStringArray(text); setValue(Arrays.asList(str)); } }
I hope this will be useful for you, otherwise let me know in order to think in other ways to address your problem.
Regards
Very nice example. Thank you very much.
I'll try this later on.
One more doubt though. In my List, I'm going to need to pass reference object to other spring beans.
ex:
Do you see any problem in doing this with a customEditor ?Code:<bean id="bean1" ... /> <bean id="bean2" ... /> <bean id="beanWithList"> <property name="listOfBeans"> <list> <ref bean="bean1" /> <ref bean="bean2" /> </list> </property> </bean>
Thanks again
I don't see any problem.
Only remember to check that the custom editor has a reference to the beanFactory in order to get the reference of the bean that you want to include in the list.
Regards