Sunday, March 21, 2010

Howto use Play! + Spring + your own properties file

For those of you who don't know Play!, Play! is a framework for developing Web applications in Java, without the adhering to the JEE specifications. It makes many things easier.

For example, Play! has a Spring integration module for effortless integration of your application with Spring.



Today I added my own properties file to my Spring-powered Play! app.

It sounds very simple, just add the following lines to your
application-context.xml:

   


and use it in your beans:

   ${myapp.somekey}


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 some time trying to figure this 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. (I am not sure this feature is 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 application's place holder prefix and suffix.
This can be done in your application-context.xml file, for example:

   
   #[
   ]



   #[myapp.somekey]

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

1 comment: