views:

69

answers:

3

is there a way to make spring (v.3.0) parse placeholders in file that are not .properties? I know I can write my own PropertyPlaceholderConfigurer but I was looking for a "cleaner" way to do it.

Thank you :)

EDIT : To be realy clear, what I want to do is to replace placeholders that are in .sql files. So the values of the placeholders are stored in .properties but the placeholders are used in .sql files.

+2  A: 

PropertyPlaceholderConfigurer can be supplied with an arbitrary Properties object (via properties property).

axtavt
+1, and then you can use, say, commons-configuration to load whatever format you want.
Bozho
+1 - I've been (ab-)using PPCs for months, and I hadn't noticed this feature.
Stephen C
+2  A: 

A PropertyPlaceholderConfigurer bean will replace placeholders in other bean definitions. Specifically, it updates the values of bean properties in bean definitions ... before the beans are actually created. Therefore, if you wanted to use PropertyPlaceholderConfigurer to modify SQL, that SQL would need to be embedded in bean property values. This class cannot replace properties in arbitrary files.

If you want to replace placeholders in arbitrary files, the PropertyPlaceholderHelper class is a better bet. For example, the method

String replacePlaceholders(String value, Properties properties)

will replace placeholders in value with properties taken from properties returning the rewritten string. You could easily adapt / wrap this to replace placeholders in a file.

Stephen C
A: 

I don't know if you use maven, but if you do, I'd use resource filtering to inject the properties into the sql files at deploy time (there are similar solutions for ant, also) and let Spring's PropertyPlaceholderConfigurer use the same property files at run time. That way everything is where it belongs (after all, the best place for properties is a .properties file).

seanizer
@seanizer - that's not deploy time. That's WAR-file-build time.
Stephen C
true. I guess I meant deploy in the sense of executing `mvn deploy`
seanizer