(一)基于xml的AOP声明式事务管理的配置方式
<?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"
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/context
http://www.springframework.org/schema/context/spring-context-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">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt" />
<!-- <!--数据源的配置方式利用DBCP的方式,需要导入commons的DBCP和POOL的jar包
最常用的DataSource的配置是采用JNDI的方式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<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="root" />
</bean>
-->
<!--采用properties文件的方式配置,通过properties文件指定数据库的连接信息 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value><!--properties文件的位置 -->
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<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>
<!--在Spring容器中 配置hibernate的sessionFactory(单例),其中class是Spring提供的注解sessionFactoryBean,为其注入DataSource属性,同时指定hibernate的相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <!-- 配置实体类(model),让 hibernate识别-->
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</value>
</list>
</property>
-->
<!-- 通过包扫描的方式,让hibernate识别实体类,就不用挨个配置实体类了 -->
<property name="packagesToScan">
<list>
<value>com.bjsxt.model</value>
</list>
</property>
<!--指定hibernate的相关属性 ,这是采用集合的注入方式(props,list,map) -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"><!--hibernate的方言 -->
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置hibernate事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置AOP,那些方法需要添加逻辑 -->
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.bjsxt.service..*.*(..))" /><!-- 切入点为service下的任何方法 -->
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" /><!--要添加的逻辑 -->
</aop:config>
<!--要添加的事务逻辑 ,指定transaction-manager -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!--设置事务的属性 ,为service下的各个方法设置的事务属性,其中方法的指定可以采用正则表达式的形式配置,更加的通用方便 -->
<tx:attributes>
<tx:method name="getUser" read-only="true" /><!--只读事务 -->
<tx:method name="add*" propagation="REQUIRED"/><!-- 默认事务属性(propagation.required):当调用方法时,如果当前方法有设置事务就使用当前事务,没有的话就创建一个新事务-->
</tx:attributes>
</tx:advice>
</beans>
(二)基于注解的方式(@Transactional)
1、在业务类或者方法上加事务的注解即可
// the service class that we want to make transactional @Transactional //在类上添加事务 public class DefaultFooService implements FooService { Foo getFoo(String fooName){//业务逻辑}; Foo getFoo(String fooName, String barName){//业务逻辑}; @Transactional //在方法上添加事务 void insertFoo(Foo foo){//业务逻辑}; void updateFoo(Foo foo){//业务逻辑}; }
2、配置文件的配置
<!-- from the file 'context.xml' --> <?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: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/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"> <!-- this is the service object that we want to make transactional --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <!-- enable the configuration of transactional behavior based on annotation配置事务驱动管理器 --> <tx:annotation-driven transaction-manager="txManager"/><!-- a PlatformTransactionManager is still required--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- (this dependency is defined somewhere else) 注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!-- other <bean/> definitions here --> </beans>
3、@Transactional的属性
@Transactional Settings
Property | Type | Description |
---|---|---|
String | Optional qualifier specifying the transaction manager to be used. | |
propagation(重要,默认值是required) | enum: | Optional propagation setting.(事务传播的属性设置) |
| enum: | Optional isolation level. |
| boolean | Read/write vs. read-only transaction |
| int (in seconds granularity) | Transaction timeout. |
| Array of | Optional array of exception classes that must cause rollback. |
| Array of class names. Classes must be derived from | Optional array of names of exception classes that must cause rollback. |
| Array of | Optional array of exception classes that must not cause rollback. |
| Array of | Optional array of names of exception classes that must not cause rollback. |