Announcement Announcement Module
Collapse
No announcement yet.
Extensible XML authoring - creating multiple beans with BeanDefinitionParser Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extensible XML authoring - creating multiple beans with BeanDefinitionParser

    I'm wondering if someone out there can provide a clear explanation of multiple bean creation via BeanDefinitionParser? I have done single bean parsers before which are very straightforward but am now trying to get my head around how to implement a more complex multi-bean use case ... and, unlike most areas of Spring, the BeanDefinitionParser APIs are a bit opaque and not so well documented.

    Expressed in abstract terms, the use case is as follows:

    I have 3 bean types (in the best tradition of programming examples, let's call them "Foo", "Bar" and "Baz") that I want to create a custom namespace for.

    The dependency relation between these beans can be expressed as:

    Code:
    Foo -has 0..*-> Baz
    Baz -has 0..1-> Bar
    As Java classes, look like:

    Code:
    public class Foo {
        public void setBazs(Collection<Baz> bazs) {
            // set all the Bazs in the application context on the Foo
        }
    }
    
    public class Baz {
    
        public void setExpression(String expr) {
            // an expression that defines this baz
        }
    
        public void setBar(Bar bar) {
            // set the Bar that this Baz belongs to
        }
    }
    
    public class Bar {
        // doesn't hold a reference to its related Bazs
    }
    In terms of the XML that would be used to configure these beans, I was picturing something along the lines of the following:

    Code:
    <my:foo>
        <my:bar>
            <my:baz expression="an expression" />
            <my:baz expression="another expression" />
        </my:bar>
        <my:bar>
            <my:baz expression="yet another expression" />
        </my:bar>
    </my:foo>
    The first question here is: is it possible (and, if so, how), when building the Foo bean definition, to build the child Bars and Bazs and set them on the Foo?

    Additionally, I was imagining that there would be alternate configuration forms, such as:

    Code:
    <my:bar id="bar1" />
    <my:foo>
        <my:baz bar-ref="bar1" expression="an expression" />
    </my:foo>
    Code:
    <my:bar id="bar1" baz-expression="an expression" />
    <my:foo />
    Dealing with the "bar-ref" attribute in the first alternate is straightforward, as we can just set that as a ref in the BeanDefinitionBuilder. What I am not so clear about at present is ...

    The second question here is: how is it possible to define that the Foo in the second alternate example should have all the Bazs created via the "baz-expression" attributes on the top-level Bars set as a property?

    Any help much appreciated! Once I've worked all of this out, I think I will write up a blog post about the solution as resources on this topic are a bit thin, as far as I can see. I'll be sure to acknowledge helpful responders!

    Thanks,

    Chris
Working...
X