繁体   English   中英

如何在控制器中定义Spring数据源?

[英]How to define Spring datasource in controller?

是否可以在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>

谢谢您的帮助 !


谢谢您的帮助 ! 但是我想我的问题很不好。

实际上,我在sync-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个站点是SOURCE(A),2个站点是TARGET(B&C); 使用窗体(由YUI制作),我应该能够同步A-> B和A-> C。
总结一下 :
1-用我的表格选择一个源和一个目标(服务器数据库),
2-我的表单发送(在Ajax中),选定的源和选定的目标到我的控制器,
3-我的控制器指向好的数据库。

做这个的最好方式是什么 ?
使用工厂? 使用setDataSource吗?
谢谢你的帮助。

您应该能够使用以下语法来实现所需的功能(请参阅Spring 2.x docs ):

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

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

因此,假设您的数据源定义正确,只需将它们注入Controller中即可:

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

如果您不想弄乱spring xml文件,并在运行时中继属性或任何其他GUI来定义那些数据源,则可以使用:

applicationContext.getBean(bean,object[]) 

请注意,使用spring并不是一种好习惯(即使有时它很方便)。 这样,您就可以定义期望构造函数参数的bean,并将这些参数作为数组的一部分提供。 这样,您可以在运行时创建任意数量的数据源,这些数据源可从任何要存储信息的位置获取。

最后,通过使用DriverManagerDataSource和setter,我可以在控制器中动态重新定义所选的dataSource(目标和源)。

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

与目标牌和所有牌桌相同。

我还看到了另一种方法: http : //blog.springsource.com/2007/01/23/dynamic-datasource-routing/ http://grails.org/Spring+Bean+Builder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM