PDA

View Full Version : multiple database URL-dependent configuration


assaf
Sep 24th, 2004, 07:45 AM
Newbie, trying to configure Spring as follows:
If the user enters the URL /customer1/list.htm
we get data from customer1's database
If the user enters /customer2/list.htm
we get data from customer2's database

Everything else about the customer1 and customer2 configuration is identical.

So, what I would like to do is map a different dispatcher servlet to each URL (simple enough), and create two different property files:
jdbc-customer1.properties
jdbc-customer2.properties

Then, somehow feed these property files into the ApplicationContext for each servlet, presumably using a PropertyPlaceholderConfigurer, with the context itself loaded from an identical XML file.

Any idea how I would go about this?
Best regards,
Assaf

assaf
Sep 28th, 2004, 06:05 AM
Rather than messing with a context's parents, I decided to leave it up to Ant to create the different company-servlet.xml files based on a master config file.

Here's the ant script:
<target name="springConfig" depends="init">
<copy tofile="${webapp}/WEB-INF/company1-servlet.xml" file="${web}/WEB-INF/master-servlet.xml" overwrite="true" />
<replace file="${webapp}/WEB-INF/company1-servlet.xml">
<replacetoken>${servlet-name}</replacetoken>
<replacevalue>company1</replacevalue>
</replace>
<copy tofile="${webapp}/WEB-INF/company2-servlet.xml" file="${web}/WEB-INF/master-servlet.xml" overwrite="true" />
<replace file="${webapp}/WEB-INF/company2-servlet.xml">
<replacetoken>${servlet-name}</replacetoken>
<replacevalue>company2</replacevalue>
</replace>
</target>

and master-servlet contains:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="location"><value>/WEB-INF/jdbc-${servlet-name}.properties</value></property>
</bean>

This allows me to keep a single Spring configuration file at source. However, it also means all of my database & transactional configuration (basically everything) is in the servlet config file instead of the the parent applicationContext.xml file. In fact, I'm thinking of doing away with the applicationContext.xml file completely on the webapp, that way I can use the single config file for JUnit tests using a similar ant task to the one shown above.

Does this seem a valid approach?
Also, if this single config file begins to get unwieldy, is there a simple way of chaining Spring config files together declaratively?

katentim
Sep 28th, 2004, 06:57 AM
Re: I can use the single config file for JUnit tests
That shouldn't be necessary. Spring supports multiple configurations which are easily useable in JUnit. See org.springframework.context.support package in Javadocs.

Re: is there a simple way of chaining Spring config files together declaratively
You can using the import feature as of Spring 1.1.1.