Spring声明式事务之代理机制与tx标签两种方式

来源:互联网 发布:阿里推荐算法负责人 编辑:程序博客网 时间:2024/04/30 09:07

Spring的事务管理分为编程式事务和声明式事务。

接下来分别以tx标签与一个dao定义一个代理类两种方式介绍

tx标签方式xml代码:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"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.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd      http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 采用dbcp的方式配置数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><!-- results in a setDriverClassName(String) call --><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/spring" /><property name="username" value="root" /><property name="password" value="123456" /></bean><!-- 使用xml管理Hibernate class="org.springframework.orm.hibernate3.LocalSessionFactoryBean,下面的class表示用Hibernate用annotation --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- property name="mappingResources" --><property name="annotatedClasses"><list><value>com.entity.User</value><value>com.entity.Log</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean><!--使用XML配置dao  --><bean id="logDao" class="com.dao.impl.LogDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="userDao" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><!--配置Service  --><bean id="userService" class="com.service.UserService"><property name="logDao"><ref bean="logDao"/></property><property name="userDao"><ref bean="userDao"/></property></bean><bean id="logService" class="com.service.LogService"><property name="logDao"><ref bean="logDao"/></property><property name="userDao"><ref bean="userDao"/></property></bean><!-- 使用tx标签进行声明式事务管理 --><bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 声明 txAdvice --><tx:advice id="txAdvice" transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with 'get' are read-only --><tx:method name="get*" read-only="true" /><!-- other methods use the default transaction settings (see below) --><tx:method name="add*" /></tx:attributes></tx:advice><aop:config><aop:pointcut expression="execution(* com.service.*Service.*(..))"id="ServiceOperation" /><aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOperation" /></aop:config></beans>

一个dao定义一个代理类的方式

xml代码如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"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.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd      http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 采用dbcp的方式配置数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><!-- results in a setDriverClassName(String) call --><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/spring" /><property name="username" value="root" /><property name="password" value="123456" /></bean><!-- 使用xml管理Hibernate class="org.springframework.orm.hibernate3.LocalSessionFactoryBean,下面的class表示用Hibernate用annotation --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- property name="mappingResources" --><property name="packagesToScan"><list><value>com.entity</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean><bean id="userDao" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="logDao" class="com.dao.impl.LogDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="userService" class="com.service.UserService"><property name="userDao"><ref bean="userDao" /></property><property name="logDao"><ref bean="logDao" /></property></bean><!-- 声明式事务管理 --><bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- Dao代理 --><bean id="userDAOProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean="txManager" /></property><property name="target"><ref bean="userDao" /></property><property name="transactionAttributes"><props><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean><bean id="logDAOProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean="txManager" /></property><property name="target"><ref bean="logDao" /></property><property name="transactionAttributes"><props><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean></beans>


0 0