jump to navigation

Spring and PropertyPlaceholderConfigurers October 22, 2008

Posted by Phill in Spring.
Tags: , , , ,
trackback

The project I’m currently working on is a fairly modular web app. We have an application context file for each module. This has worked pretty well so far. However, I ran into an issue today when I wanted to add a new module: I kept getting the issue ‘Could not resolve placeholder ‘(property name)’.

I thought this was a bit strange because I thought I’d set up the property configurer correctly, and the properties file was definitely in the right place.

It turns out (via a bit of Googling – see this thread) that you don’t need multiple PropertyPlaceholderConfigurers. If one is defined, it will load properties for the whole of your application context files (as long as they’re within the same overall context).

If you have multiple PropertyPlaceholderConfigurers defined, what will happen is that if one of them can’t find a specific property, it will treat it as a fatal exception. This is of course going to happen if your different configurers point to different properties files!

There are three solutions:

  • Set the “ignoreUnresolvablePlaceholders” attribute on all property placeholder;
  • Set the “placeholderPrefix” or “placeholderSuffix” properties individually for each one so they don’t all match ${…}
  • Use one property placeholder configurer and just point it to all your properties files.

I’ve gone with the last option at the moment – it’s the best solution for what we need at the moment.

About these ads

Comments

1. LCB - December 16, 2008

Agreed. I ran into this yesterday and today, and through experimentation, reading your blog, and looking at the stack trace, found that I only needed the one PPC( property placeholder configurer) defined in any one of the contexts (I have four or five defined in the context-param for TOmcat).

When I tried to define another I ran into problems and decided that since my properties belonged in one of the property files already defined that I would just use the one PPC. I gather multiples are possible, but not necessary for my use at the moment. For reuse purposes it would be good to eventually figure out how to setup PPCs for each context so they can be used standalone in other apps.

The first solution seems like it would cause problems because unresolved placeholders would go undetected. The second solution (seen elsewhere too) seems like a hack IMO.

Interestingly, the servlet context, which is referenced in the element of web.xml, is loaded later and needs its own PPC.


Sorry comments are closed for this entry

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: