Announcement Announcement Module
No announcement yet.
multiple database URL-dependent configuration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple database URL-dependent configuration

    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:

    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,

  • #2
    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="$&#123;webapp&#125;/WEB-INF/company1-servlet.xml" file="$&#123;web&#125;/WEB-INF/master-servlet.xml" overwrite="true" />
    	<replace file="$&#123;webapp&#125;/WEB-INF/company1-servlet.xml">
    	<copy tofile="$&#123;webapp&#125;/WEB-INF/company2-servlet.xml" file="$&#123;web&#125;/WEB-INF/master-servlet.xml" overwrite="true" />
    	<replace file="$&#123;webapp&#125;/WEB-INF/company2-servlet.xml">
    and master-servlet contains:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location"><value>/WEB-INF/jdbc-$&#123;servlet-name&#125;.properties</value></property>
    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?


    • #3
      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 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.