<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置属性占位符配置器 --> <bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 配置DBCP数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.mysql.className}"/> <property name="url" value="${jdbc.mysql.url}"/> <property name="username" value="${jdbc.mysql.user}"/> <property name="password" value="${jdbc.mysql.password}"/> </bean> <!-- 配置SessionFactory,如果Hibernate的映射方式采用注解,那么SessionFactory对象的bean为AnnotationSessionFactoryBean 如果映射文件采用的是.hbm.xml可以使用该bean也可以使用LocalSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <!-- 配置Hibernate属性,如二级缓存的开启关闭,查询缓存的开启关闭、数据库方言等 --> <props> <prop key="hibernate.dialect">${jdbc.mysql.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/sms/entity/Student.hbm.xml</value> <value>com/sms/entity/Course.hbm.xml</value> <value>com/sms/entity/Teacher.hbm.xml</value> </list> </property> </bean> <!-- 1.配置Hibernate事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 2.事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="do*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3.哪些类哪些方法使用事务 --> <aop:config> <aop:pointcut id="allServiceMethod" expression="* com.sms.service.impl.*Impl.*(..)"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod"/> </aop:config> <!-- 配置HibernateTemplate以简化Hibernate应用的开发--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
execution(* com.sms.service.impl.*Impl.*(..))
表示com.sms.service.impl包下所有的类的所有方法。
第一个*代表所有的返回值类型。
第二个*代表所有的以Impl结尾的类
第三个*代表类所有的方法
最后一个代表所有的参数。
xml配置文件说明~!
实施事务的步骤
1.定义资源,DataSource
2.定义事务管理器(定义资源的事务)
3.定义事务通知:定义如何实施事务(实施事务的方法名和对应的事务属性)
需要使用事务管理器管理事务,定义了如何选择目标对象的方法及实施的事务属性。
4.定义advisor(切入点和事务通知):切入点选择需要实施事务的目标对象(一定是业务逻辑层)
5.Spring织入事务通知到目标对象(AOP代理)
环绕通知
Before: 开启事务
业务逻辑
After: 提交或回滚事务
本质
1.利用模板设计模式+aop将不可以变的事务管理提取出去。
2.利用环绕通知实施事务的开启和关闭。
3.需要Spring一致的异常体系支持。