Howto tutorial: use Play + Spring + your own properties file

819 views
Skip to first unread message

Oded Peer

unread,
Mar 21, 2010, 7:02:39 AM3/21/10
to play-framework
Today I added my own properties file to my Spring-powered Play app.
It sounds very simple, simply add the following lines to your
application-context.xml:

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myapp.properties"/>
</bean>

and use it in your beans:

<bean id="a" class="myapp.A">
<property name="str"><value>${myapp.somekey}</value></property>
</bean>

However, when you run your application you get the error:
Invalid bean definition with name 'a' defined in resource loaded
through SAX InputSource: Could not resolve placeholder
'myapp.somekey'.

I spent almost 3 hours trying to figure out. Urrrghhhh.
Finally, I understood why this happens and how to avoid it.

The Play! Spring module uses a GenericApplicationContext and
problematically adds a PropertyPlaceholderConfigurer to it, allowing
you to substitute place holders with values in your application.conf
file. (is this documented anywhere?)
Now, when Spring invokes
AbstractApplicationContext.invokeBeanFactoryPostProcessors() it
invokes Play's PropertyPlaceholderConfigurer, which tries to convert $
{myapp.somekey} as well.
Since it can't find ${myapp.somekey} in application.conf the exception
is thrown.
The way to "fool" the PropertyPlaceholderConfigurer Play! adds is to
override your applications place holder prefix and suffix.
This can be done in your application-context.xml file, for example:

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myapp.properties"/>
<property name="placeholderPrefix"><value>#[</value></property>
<property name="placeholderSuffix"><value>]</value></property>
</bean>

<bean id="a" class="myapp.A">
<property name="str"><value>#[myapp.somekey]</value></property>
</bean>

Now your application can read properties from any properties file you
want.

Oded

Guillaume Bort

unread,
Mar 21, 2010, 2:00:56 PM3/21/10
to play-fr...@googlegroups.com
Thank you we will add it to the spring module documentation.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages