Simple Spring trick - Splitting config files
The application that I am working on at work uses Spring fairly heavily for its configuration. One problem that we have is that we don’t want to expose the config file, but we need to expose the data source part of it. We are configuring Hibernate through Spring which requires us to put the data source information in the Spring config file; we needed a way to split the config file so that data source part could exist outside the jar file.
This actually proved really easy to do, trivial actually, but I will post the method for other newbies, like me, who may stumble upon this blog.
Basically, you split the file into two complete Spring configuration files and then import the one into the other. There are two keys to remember when doing this: 1) both files must be complete config xml files that validate against the Spring schema and 2) the path used when importing is relative to the parent file so the two files must be in the same directory or on the same classpath.
Here is the code to use (shortened for brevity):
There is one more rule that you must remember. You have to reference the beans in the imported file using <ref bean="bean id"/> instead of <ref local="my bean"/> because the @local@ attribute is scoped to the physical file in which it appears. Also, you need to import files before referencing any beans in the imported file (this is probably obvious though).
Simple, eh? I love Spring.