title: How to define Spring datasource in controller?
body: <p>Is it possible to define a datasource connector in a Spring controller ?</p>
  
  <p>I'm working on a tool : synchronize a source table to a target table.<br>
  I would define source and target in my controller (to synchronize different databases - in my view I can select different source and target databases).</p>
  
  <p>Actually, I define my datasource in file call : datasource.xml</p>
  
  <p>My code :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;bean id="sourceDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/source"/&gt;
          &lt;!--&lt;property name="url" value="jdbc:mysql://linkSource"/&gt;--&gt;
          &lt;property name="username" value="username"/&gt;
          &lt;property name="password" value="password"/&gt;
      &lt;/bean&gt;
  
          &lt;bean id="targetDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/target"/&gt;
          &lt;!--&lt;property name="url" value="jdbc:mysql://linkTarget"/&gt;--&gt;
          &lt;property name="username" value="username"/&gt;
          &lt;property name="password" value="password"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Thank you for your help !</p>
  
  <hr>
  
  <p>Thank you for your help !
  But I think I put my question badly.</p>
  
  <p>Actually, I have in my sync-servelt.xml (just part) :</p>
  
  <pre><code>        &lt;!--sync query beans--&gt;
          &lt;bean id="sourceDatasetQueryBean" class="ds.sync.db.SyncDatasetQuery" name="sourceDatasetsQuery"&gt;
              &lt;property name="dataSource" ref="sourceDataSource"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="targetDatasetQueryBean" class="ds.sync.db.SyncDatasetQuery" name="targetDatasetsQuery"&gt;
              &lt;property name="dataSource" ref="targetDataSource"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="sourceDatasetDescriptionQueryBean" class="ds.sync.db.SyncDatasetDescriptionQuery" name="sourceDatasetsDescriptionQuery"&gt;
              &lt;property name="dataSource" ref="sourceDataSource"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="targetDatasetDescriptionQueryBean" class="ds.sync.db.SyncDatasetDescriptionQuery" name="targetDatasetsDescriptionQuery"&gt;
              &lt;property name="dataSource" ref="targetDataSource"/&gt;
          &lt;/bean&gt;
  ...more...
  </code></pre>
  
  <p>And, in my controller I'm using :</p>
  
  <pre><code> @Autowired
   @Qualifier("sourceDatasetQueryBean")
   protected SyncDatasetQuery m_datasetQuerySource;
  
   @Autowired
   @Qualifier("targetDatasetQueryBean")
   protected SyncDatasetQuery m_datasetQueryTarget;
  
   @Autowired
   @Qualifier("sourceDatasetDescriptionQueryBean")
   protected SyncDatasetDescriptionQuery m_datasetDescriptionQuerySource;
  
   @Autowired
   @Qualifier("targetDatasetDescriptionQueryBean")
   protected SyncDatasetDescriptionQuery m_datasetDescriptionQueryTarget;
  ...more...
  </code></pre>
  
  <p>I have 11 tables to sync between source and target...<br>
  Is there a way to group my query beans ?</p>
  
  <p>My synchronizations must be performed on several databases.<br>
  For example, I have 3 sites in different places, 1 site is SOURCE (A), 2 sites are TARGET (B &amp; C) ; with a form (made with YUI), I should be able to sync A->B and A->C.<br>
  To sum up :<br>
  1- with my form I select a SOURCE, and a TARGET (serveral databases),<br>
  2- my form send (in Ajax), the selected SOURCE and selected TARGET to my controller,<br>
  3- my controller points to the good database.</p>
  
  <p>What is the best way to do this ?<br>
  Using a Factory ?
  Using setDataSource ?<br>
  Thank you for help.  </p>