6.spring中事务管理
声明式的事务管理(Declarative transaction management):
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:XE</value>
</property>
<property name="username">
<value>rose</value>
</property>
<property name="password">
<value>aier</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">15</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/kettas/shops/entity/Entity.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernatetemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--
dao的bean
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<bean id="userDao" class="edu.yzu.shops.dao.impl.UserDaoImpl">
<property name="hibernateTemplate">
<ref local="hibernatetemplate" />
</property>
</bean>
<!--
biz的配置
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<bean id="userBiz" class="edu.yzu.shops.biz.impl.UserBizImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<!--
事物控制
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="delete*">
PROPAGATION_REQUIRED,-Exception
</prop>
</props>
</property>
</bean>
<!-- 有两种方式可以给一个bean加上事务控制,一种为自动创建。
另一种是指明创建 .第一种为:-->
<bean id="autoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>userBiz</value>
<value>productBiz</value>
<value>orderBiz</value>
<value>orderItemBiz</value>
<value>categoryBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 第二种方法为下面,显然表比较少时用第二种可以,但是当表比较多时显然第一种更合适。 -->
<bean id="userBizProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target">
<ref bean="userBiz" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 下面的设置表时要代理的不是类本身,而是其实现的接口 -->
<property name="proxyTargetClass">
<value>false</value>
</property>
<property name="proxyInterfaces">
<list>
<value>edu.yzu.biz.UserBiz</value>
</list>
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="delete*">
PROPAGATION_REQUIRED,-Exception
</prop>
</props>
</property>
</bean>
<!--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
1.
2.
3.
4.
5.
6.
7. <bean
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器 -->
<property>
<ref bean="transactionManager" />
</property>
<!-- 此属性指定目标类本身是否是代理的对象,如果目标类没有实现任何类,就设为true代表自己 -->
<property>
<value>false</value>
</property>
<property>
<value> com.test.service.userManageService</value>
</property>
<!-- 目标bean -->
<property>
<ref bean="userManageService"/>
</property>
<!-- 配置事务属性 -->
<property>
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
利用继承的思想简化配置,适合相对比较多的模块时使用。
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
8.<bean
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property>
<ref bean="transactionManager" />
</property>
<!-- 配置事务属性 -->
<property>
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
而具体的模块可以简单的这样配置。只要指明它的parent(父类)就可以了。父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。
Java代码
<bean id="userManageServiceProxy" parent="transactionBase" >
<property name="target">
<ref bean="userBiz"/>
</property>
</bean> <bean parent="transactionBase" >
<property>
<ref bean="userManageService"/>
</property>
</bean>