您的位置:首页 > 编程语言 > Java开发

Spring环境中支持多数据库源连接的配置及使用方法

2009-05-25 20:59 691 查看
Spring环境中支持多数据库源连接的配置及使用方法
可加载xml配置文件中包含以下bean内容。
<beans>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Class name for the Connector/J driver -->
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db-ip-hostname)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = service_name)
<!--sid = sid_name -->
)
)</value>
</property>
<property name="username">
<value>user</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<!--business dao bean-->
<bean id="businessDao1" class="com.example.BusinessDao1">
<property name="jdbcTemplate" ref="jdbcTemplate1"></property>
</bean>
<bean id="businessDao2" class="com.example.BusinessDao2">
<property name="jdbcTemplate" ref="jdbcTemplate1"></property>
</bean>
<bean id="businessDao3" class="com.example.BusinessDao1">
<property name="jdbcTemplate" ref="jdbcTemplate3"></property>
</bean>
<bean id="businessDao4" class="com.example.BusinessDao4">
<property name="jdbcTemplate" ref="jdbcTemplate1"></property>
</bean>
<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager1"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="import*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>
businessDao1, businessDao2,
businessDao3, businessDao4
</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor1</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor1"/>
</bean>
</beans>
通过改变上面的dataSource1, jdbcTemplate1,transactionManager1以及自动代理bean的内容即可复制成其他数据库的一套配置。所有businessbean不需要重新写,java代码也不需要改动,是一个较为省事的多数据库支持方案。如果需要同时支持mysql/oracle等不同类型数据库,也只是sql语句加载的问题,事务管理已经被简单包装进了。(不同数据库类型加载不同sql语句,只需要为businessbean加dbtype属性并在xml里配入即可。)
需要注意的一点,如果business dao 有实现interface,则引用的地方必须也用interface名声明,否则无法cast。这个是动态代理实现机制的原因,因为CGlib机制会cast成一个子接口返回。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐