2009-10-13 12 views
0

是否可以在Spring控制器中定義數據源連接器?如何在控制器中定義Spring數據源?

我工作的一個工具:源表同步到目標表。
我會在我的控制器中定義源和目標(以同步不同的數據庫 - 在我看來,我可以選擇不同的源和目標數據庫)。

其實,我定義我的數據源文件調用:datasource.xml

我的代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <context:annotation-config /> 

    <bean id="sourceDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost/source"/> 
     <!--<property name="url" value="jdbc:mysql://linkSource"/>--> 
     <property name="username" value="username"/> 
     <property name="password" value="password"/> 
    </bean> 

     <bean id="targetDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost/target"/> 
     <!--<property name="url" value="jdbc:mysql://linkTarget"/>--> 
     <property name="username" value="username"/> 
     <property name="password" value="password"/> 
    </bean> 

</beans> 

謝謝您的幫助!


謝謝你的幫忙! 但我覺得我的問題很糟糕。

其實,我有我的同步servelt.xml(只是一部分):

 <!--sync query beans--> 
     <bean id="sourceDatasetQueryBean" class="ds.sync.db.SyncDatasetQuery" name="sourceDatasetsQuery"> 
      <property name="dataSource" ref="sourceDataSource"/> 
     </bean> 

     <bean id="targetDatasetQueryBean" class="ds.sync.db.SyncDatasetQuery" name="targetDatasetsQuery"> 
      <property name="dataSource" ref="targetDataSource"/> 
     </bean> 

     <bean id="sourceDatasetDescriptionQueryBean" class="ds.sync.db.SyncDatasetDescriptionQuery" name="sourceDatasetsDescriptionQuery"> 
      <property name="dataSource" ref="sourceDataSource"/> 
     </bean> 

     <bean id="targetDatasetDescriptionQueryBean" class="ds.sync.db.SyncDatasetDescriptionQuery" name="targetDatasetsDescriptionQuery"> 
      <property name="dataSource" ref="targetDataSource"/> 
     </bean> 
...more... 

而且,在我控制我使用:

@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... 

我有11個表同步源和目標之間...
有沒有一種方法來分組我的查詢bean?

我必須同步在多個數據庫中進行。例如,我在不同的地方有3個站點,1個站點是源(A),2個站點是TARGET(B & C);例如,我在不同的地方有3個站點,1個站點是源(A),2個站點是目標(B & C);與表格(用YUI製作),我應該能夠同步A-> B和A-> C。
綜上所述:
1-與我的形式I選擇一個源和目標(serveral的數據庫),
2-我的形式發送(Ajax中),所選擇的源和選擇的目標到我的控制器,
3-我的控制器指向好的數據庫。

這樣做的最好方法是什麼?
使用工廠? 使用setDataSource?
謝謝你的幫助。

回答

0

最後,通過使用DriverManagerDataSource和,並使用設定器,我可以重新定義選擇我的數據源(目標和源)動態地在我的控制器。

我只需要使用: setDriverManagerDataSource(m_sourceDataSource); 和m_datasetQuerySource.setDataSource(dataSource); (SOURCE)

與目標和所有表同樣的發揮。

我看到還有其他的方式來做到這一點: http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/ http://grails.org/Spring+Bean+Builder

0

您應該可以使用下面的語法來實現你想要的(見Spring 2.x docs):

@Autowired 
@Qualifier("targetDataSource") 
DataSource targetDataSource; 

@Autowired 
@Qualifier("sourceDataSource") 
DataSource sourceDataSource; 
0

因此,假設您的數據源定義正確的,這只是它們注入到控制器中的一個問題:

<bean id="myController" class="..."> 
    <property name="sourceDS" ref="sourceDataSource" /> 
    <property name="targetDS" ref="targetDataSource" /> 
    .... 
</bean> 
+0

如果我有一個以上的目標更多(可能是5個目標),什麼是最好的在我的控制器,選擇好目標? 工廠是個好主意嗎? –

0

如果你不想使用Spring XML文件搞亂,並且在性能或任何其他GUI定義在運行時的數據源接力,您可以使用:

applicationContext.getBean(bean,object[]) 

請注意,春季不是一個好的做法(即使它有時非常方便)。 通過這種方式,您可以定義期望構造函數參數的bean,並將這些參數作爲數組的一部分提供。這樣,您可以在運行時創建儘可能多的數據源,以便從要存儲信息的任何位置獲取這些數據源。

相關問題