java 如何在控制器中定义 Spring 数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1561209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define Spring datasource in controller?
提问by Fabien Barbier
Is it possible to define a datasource connector in a Spring controller ?
是否可以在 Spring 控制器中定义数据源连接器?
I'm working on a tool : synchronize a source table to a target table.
I would define source and target in my controller (to synchronize different databases - in my view I can select different source and target databases).
我正在开发一个工具:将源表同步到目标表。
我会在我的控制器中定义源和目标(以同步不同的数据库 - 在我看来,我可以选择不同的源和目标数据库)。
Actually, I define my datasource in file call : datasource.xml
实际上,我在文件调用中定义了我的数据源:datasource.xml
My code :
我的代码:
<?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>
Thank you for your help !
谢谢您的帮助 !
Thank you for your help ! But I think I put my question badly.
谢谢您的帮助 !但我认为我的问题很糟糕。
Actually, I have in my sync-servelt.xml (just part) :
实际上,我在我的 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...
And, in my controller I'm using :
而且,在我的控制器中,我正在使用:
@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...
I have 11 tables to sync between source and target...
Is there a way to group my query beans ?
我有 11 个表要在源和目标之间同步......
有没有办法对我的查询 bean 进行分组?
My synchronizations must be performed on several databases.
For example, I have 3 sites in different places, 1 site is SOURCE (A), 2 sites are TARGET (B & C) ; with a form (made with YUI), I should be able to sync A->B and A->C.
To sum up :
1- with my form I select a SOURCE, and a TARGET (serveral databases),
2- my form send (in Ajax), the selected SOURCE and selected TARGET to my controller,
3- my controller points to the good database.
我的同步必须在多个数据库上执行。
例如,我在不同地方有 3 个站点,1 个站点是 SOURCE (A),2 个站点是 TARGET (B & C);使用表单(用 YUI 制作),我应该能够同步 A->B 和 A->C。
总结一下:
1-用我的表单选择一个源和一个目标(服务器数据库),
2-我的表单发送(在 Ajax 中),选择的源和选择的目标到我的控制器,
3-我的控制器指向好的数据库。
What is the best way to do this ?
Using a Factory ?
Using setDataSource ?
Thank you for help.
做这个的最好方式是什么 ?
使用工厂?使用 setDataSource 吗?
谢谢你的帮助。
采纳答案by Fabien Barbier
Finally, by using DriverManagerDataSource, and using setter, I can redefine my dataSource selected (target and source) dynamically in my controller.
最后,通过使用 DriverManagerDataSource 和 setter,我可以在我的控制器中动态地重新定义我选择的数据源(目标和源)。
I just need to use : setDriverManagerDataSource(m_sourceDataSource); and m_datasetQuerySource.setDataSource(dataSource); (SOURCE)
我只需要使用: setDriverManagerDataSource(m_sourceDataSource); 和 m_datasetQuerySource.setDataSource(dataSource); (来源)
Same play with target and all tables.
与目标和所有表相同的游戏。
I see also other way to do that : http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/http://grails.org/Spring+Bean+Builder
我也看到了其他方法:http: //blog.springsource.com/2007/01/23/dynamic-datasource-routing/ http://grails.org/Spring+Bean+Builder
回答by Rich Kroll
You should be able to use the following syntax to achieve what you want (see Spring 2.x docs):
您应该能够使用以下语法来实现您想要的(请参阅Spring 2.x 文档):
@Autowired
@Qualifier("targetDataSource")
DataSource targetDataSource;
@Autowired
@Qualifier("sourceDataSource")
DataSource sourceDataSource;
回答by Gandalf
So assuming your data sources are defined correctly it's only a matter of injecting them into your Controller:
因此,假设您的数据源定义正确,只需将它们注入您的控制器:
<bean id="myController" class="...">
<property name="sourceDS" ref="sourceDataSource" />
<property name="targetDS" ref="targetDataSource" />
....
</bean>
回答by pedromarce
If you don't want to be messing with spring xml files, and relay in properties or any other GUI to define those datasources at runtime, you might use:
如果您不想弄乱 spring xml 文件,并且不想在属性或任何其他 GUI 中中继以在运行时定义这些数据源,则可以使用:
applicationContext.getBean(bean,object[])
Be aware that is not a good practise with spring (even that it is quite handy sometimes). This way you define your beans expecting constructor arguments and supply those arguments as part of the array. This way you create as many datasources you need at runtime getting those from wherever you want to store the information.
请注意,这对 spring 来说不是一个好习惯(即使它有时非常方便)。通过这种方式,您可以定义需要构造函数参数的 bean,并将这些参数作为数组的一部分提供。通过这种方式,您可以在运行时创建尽可能多的数据源,从您想要存储信息的任何位置获取这些数据源。

