tags:

views:

186

answers:

1

Santa's got a list. He wants to make a "present" for every child on hist list (we're assuming for the moment they're all good) and load them all in his sled. Can he do it with Spring?

I've written a simple FileListFactory that implements FactoryBean and returns a list of strings that represents the lines in a given file. Set that aside for now - if we solve the problem for a <list>, then I assume I can plug in a ref to this factory's output in its place.

What I want is a list of "foo" objects. The foo objects are non-singleton instances of some bean (likely with an abstract bean definition). Each foo object created gets one of the list items as a property.

To up the ante one level further, the property in question is of a "bar" object. I have already created a property editor to make "bar" objects from strings. So the thing that iterates over the list of strings should use the property editor infrastructure when setting the per-instance property.

It seems like there ought to be some sort of list factory iterator bean or something for this. The problem appears to be that all of the searches I'm doing to look for example solutions to this fail because terms like "list" are far too generic.

A: 

If I understood you correctly, you have a list of strings (read from a file, for instance). For each of this strings you want to create a "foo" object where the given string is assigned to the "bar" property, with the appropriate conversion.

You can achieve this by implementing an own BeanFactoryPostProcessor. For each of the strings, create a new BeanDefinition and register it with the target factory. To simplify things, create and register a ChildBeanDefinition, providing the name of the "parent" bean with all the generic configuration and only setting the "bar" property.

lexicore
You've got the problem statement correct, but"For each of the strings, create a new BeanDefinition"How do you do that?
nsayer
new ChildBeanDefinition("myAbstractParentBean")See BeanDefinition, BeanDefinitionRegistry and implementations.
lexicore
oh, I see. The BFPP takes the list as a property and does this in code. I wound up doing something similar to solve this, but with ordinary beans. I have a bar list object plugged into a special "FooListCreator." That gizmo is BeanFactoryAware and asks for an instance of "foo" for each "bar" in the list and then programmatically sets the bar property. I was just hoping that more of this was doable without actual Java code - that is, in the application context xml files themselves.
nsayer
I don't think it's possible without coding, but I'm almost sure that once you have such a postprocessor, you car reuse it. Then in will be configuration only, like you want.
lexicore