Results 1 to 8 of 8

Thread: PropertiesBeanDefinitionReader List Attribute

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    8

    Default PropertiesBeanDefinitionReader List Attribute

    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

  2. #2
    Join Date
    May 2010
    Location
    Lima, Perú
    Posts
    26

    Default

    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

  3. #3
    Join Date
    Jan 2010
    Posts
    8

    Default

    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 ?

  4. #4
    Join Date
    May 2010
    Location
    Lima, Perú
    Posts
    26

    Default

    Check this.
    It seems that it was requested before but was not done.

    https://jira.springsource.org/browse...3Aall-tabpanel

  5. #5
    Join Date
    Jan 2010
    Posts
    8

    Default

    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

  6. #6
    Join Date
    May 2010
    Location
    Lima, Perú
    Posts
    26

    Default

    Hi,
    You can solve your problem using customEditors.
    Following there is an example

    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));
            }
        }
    In this example I am using the ListTypeEditor in order to set properly the attributes of type List.

    I hope this will be useful for you, otherwise let me know in order to think in other ways to address your problem.
    Regards

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •