views:

410

answers:

3

Hi,

All the examples I have seen where the PropertyPlaceHolderConfigurer is used seem to be setting simple values like Strings and ints.

How do you use the PPC to set the values of classes.

E.g. If i had a class signature Source(String name, DistributionSample batch, DistributionSample delay)

How would I go about setting the batch and delay properties.

There is also a small catch. DistributionSample is an abstract class. On the bright side, The class that is using the propertyPlaceHolder knows the beanName of the "Solid" class that needs to be instantiated.

Any help would be much appreciated.

A: 

As you said, PropertyPlaceHolderConfigurer only works for String values which String can convert into the target type.

So you can either tell Spring how to convert a String into DistributionSample objects, or you can use the property placeholders to refer to bean names,e.g.

<bean class="Source">
   <constructor-arg value="source name"/>
   <constructor-arg ref="${batch.beanName}"/>
   <constructor-arg ref="${delay.beanName}"/>
</bean>

<bean id="batch" class="....."/>
<bean id="delay" class="....."/>

If your properties file contains

batch.beanName = batch
delay.beanName = delay

Then the bean references will be resolved. You can also use property placeholder syntax for bean classes, if that's useful for you:

<bean id="sample" class="${batch.classname}/>
skaffman
I considered doing it that way, but then the next issue is with giving it the arguments for the batch/delay beans class.I.e. Inside the source bean can I have <constructor-arg ref="${batch.beanName}"/> where the beanName will be resolved to the sample i want to use, e.g. "Erlang". And then have a bean <bean id="Deterministic" class=".."/><constructor-arg ref="${exp.prop1}"/></bean>Where I can provide the k values for that bean in my property files as well.
Looking into the converter though. Only problem is that I'm currently using Spring 2.5. with flex and blazeds. I'm weary of breaking things as its hardly ever a simple case of changing jars....
@babyangel86: The mechanism is different is Spring 2.5.6, but still works: http://static.springsource.org/spring/docs/2.5.6/reference/validation.html#beans-beans-conversion-customeditor-registration
skaffman
A: 

Have a look at the PropertyOverrideConfigurer when you want to override properties of specific beans whose assembling you have no control of.

Calm Storm
A: 

In the end it made more sense to use Castor to map the XML to the java objects.

Castor integrates with spring, so the beans can be instantiated from the Castor marshallers.

Thanks for all the hints and tips guys.