Java 如何在控制器中定义Spring数据源?
可以在Spring控制器中定义数据源连接器吗 我正在开发一个工具:将源表同步到目标表。 我会在控制器中定义源和目标以同步不同的数据库-在我的视图中,我可以选择不同的源和目标数据库 实际上,我在文件调用中定义了我的数据源:datasource.xml 我的代码:Java 如何在控制器中定义Spring数据源?,java,spring,jdbc,spring-mvc,controller,Java,Spring,Jdbc,Spring Mvc,Controller,可以在Spring控制器中定义数据源连接器吗 我正在开发一个工具:将源表同步到目标表。 我会在控制器中定义源和目标以同步不同的数据库-在我的视图中,我可以选择不同的源和目标数据库 实际上,我在文件调用中定义了我的数据源:datasource.xml 我的代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns
<?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>
我有11个表要在源和目标之间同步。。。
有没有办法对我的查询bean进行分组
我的同步必须在多个数据库上执行。
例如,我在不同的地方有3个站点,1个站点是源A,2个站点是目标B&C;使用YUI制作的表单,我应该能够同步a->B和a->C。
总而言之:
1-使用我的表单选择源和目标服务器数据库,
2-我的表单将Ajax、选定源和选定目标发送到我的控制器,
3-我的控制器指向良好的数据库
最好的方法是什么?
使用工厂?
使用setDataSource?
谢谢你的帮助 您应该能够使用以下语法来实现希望看到的内容:
因此,假设数据源定义正确,只需将它们注入控制器即可:
<bean id="myController" class="...">
<property name="sourceDS" ref="sourceDataSource" />
<property name="targetDS" ref="targetDataSource" />
....
</bean>
如果您不想弄乱spring xml文件,不想在运行时在属性或任何其他GUI中中继来定义这些数据源,您可以使用:
applicationContext.getBean(bean,object[])
请注意,使用spring不是一个好的练习,即使它有时非常方便。
通过这种方式,您可以定义需要构造函数参数的bean,并将这些参数作为数组的一部分提供。通过这种方式,您可以在运行时创建所需的任意多个数据源,并从您希望存储信息的任何位置获取这些数据源。最后,通过使用DriverManager数据源和setter,我可以在控制器中动态重定义数据源选定的目标和源 我只需要使用: setDriverManagerDataSourcem_sourceDataSource; 和m_datasetQuerySource.setDataSourcedataSource;来源 对目标和所有桌子都一样 我还看到了其他方法:
如果我有多个目标,可能是5个目标,那么在控制器中选择好目标的最佳方法是什么?工厂是个好主意吗?
@Autowired
@Qualifier("targetDataSource")
DataSource targetDataSource;
@Autowired
@Qualifier("sourceDataSource")
DataSource sourceDataSource;
<bean id="myController" class="...">
<property name="sourceDS" ref="sourceDataSource" />
<property name="targetDS" ref="targetDataSource" />
....
</bean>
applicationContext.getBean(bean,object[])