开发者

Make Spring replace placeholders in different file type (other than Spring's configs)

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

EDIT:

开发者_StackOverflow中文版

To be more specific, what I actually want to do is to replace placeholders present in .sql files. So the values of the placeholders are stored in .properties but the placeholders are used in .sql files.


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.


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜