spring2.5 与 ibiats 2.x , hibernate 3.x 的事务整合配置

[/code]一: [color=red]spring2.5[/color] 与 [color=red]ibiats 2.x[/color] 的 配置整合

1.WEB-INF目录下的jdbc.properties[color=green](WEB-INF/jdbc.properties)[/color]


jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.url=jdbc:jtds:sqlserver://192.168.1.3:1433/club
jdbc.username=club
jdbc.password=club


2.WEB-INF目录下的[color=green]applicationContext.xml[/color][color=green](WEB-INF/applicationContext.xml)[/color]

<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 读取数据源properties文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- <value>WEB-INF/mail.properties</value> -->
<value>WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>

<!-- 设置数据源链接参数 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!-- 配置Ibiats的事务管理器 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 定义事务拦截器bean -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">

<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<!-- Service -->
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="post*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="deny*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="agree*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="bar*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="join*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="Approve*">PROPAGATION_REQUIRED,-Exception</prop>
<!--DAO-->
<prop key="search*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
</props>
</property>
</bean>

<!-- 定义BeanNameAutoProxyCreator-->
<bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>


二:[color=red]spring2.5[/color] 与 [color=red]Hibernate 3.x[/color] 的 配置整合


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/myuser</value>
</property>
<property name="username">
<value>lighter</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/mmc/dao/domain/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置Hibernate的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 定义事务拦截器bean -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<!-- 所有名字以DAO,Service结尾的bean,将由该"bean后处理器"为其创建事务代理;实际上应该在业务层进行事务管理,这里只是举一个简单例子 -->
<value>*DAO,*Service</value>
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 可以增加其他的拦截器 -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!-- 举下面这一个例子:这时候,这一个bean已经有了事务管理,可以增加类似的bean -->
<bean id="searchUserDAO" class="org.mmc.dao.impl.SearchUserDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>


下面再看一种也是常用的使用bean继承简化事务代理配置

<bean id="test1" class="test.TransactionTestImpl">
<property name="ds"><ref local="dataSource"/></property>
</bean>

<bean id="test2" class="test.TestImpl">
<property name="ds"><ref local="dataSource"/></property>
</bean>

<bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id="testTrans1" parent="baseTxProxy">
<property name="target">
<ref local="test1"/>
</property>
</bean>
<bean id="testTrans2" parent="baseTxProxy">
<property name="target">
<ref local="test2"/>
</property>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值