Using Noah to Configure Properties in your Spring Container
On a recent trip through our code base, I realized that there were multiple projects that used identical properties defined in files that sat on the file system. I find DRY is a superior pattern to copy and paste I wanted to consolidate these into a single location.
One approach would be to move them up a level in the source stack, and either rely on the properties being included in a jar or included via the build process. Both of these approaches suffer from some drawbacks and require rebuilds on a config change. Also the properties are still located all over the place, making it difficult to recover from mistakes or handle changes to the environment.
One of my co-workers, John Vincent, is currently developing Noah which is a lightweight node/service registry. However, there’s nothing that says you couldn’t use it as a centralized location for properties files.
I uploaded my properties file using Noah’s Ephemeral API. From there, it was a very trivial to make the properties available to Spring:
However, I didn’t like the url hardcoded in the configuration file. I’d prefer to have them configured through some properties that do live on the filesystem. In effect, each server would just need to know where to go to configure themselves. This required more advanced Spring trickery. Since all PropertyPlaceholderConfigurer instances implement PriorityOrder, they will be implemented before any property expansion has become. Therefore, you have to create your own Ordered bean that wraps a PropertyPlaceholderConfigurer. The following code is adapted from this discussion forum.
Then you configure it in your applicationContext.xml file like the following:
Now, you just have to manage one file on the filesystem that points to where the rest of the application’s configs can be found.
13 Notes/ Hide
-
tejustechblog posted this