Announcement Announcement Module
Collapse
No announcement yet.
multiple database URL-dependent configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    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

  • #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:
    Code:
    <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">
    		<replacetoken>$&#123;servlet-name&#125;</replacetoken>
    		<replacevalue>company1</replacevalue>
    	</replace>
    	<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">
    		<replacetoken>$&#123;servlet-name&#125;</replacetoken>
    		<replacevalue>company2</replacevalue>
    	</replace>
    </target>
    and master-servlet contains:
    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location"><value>/WEB-INF/jdbc-$&#123;servlet-name&#125;.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?

    Comment


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

      Comment

      Working...
      X