事务处理
第一种方式:
首先配置datasoure:
<bean id="myDataSource"
?????? class="org.apache.commons.dbcp.BasicDataSource"
?????? destroy-method="close">
?????? <property name="driverClassName">
?????????? <value>com.mysql.jdbc.Driver</value>
?????? </property>
?????? <property name="url">
?????????? <value>jdbc:mysql://localhost:3306/punish</value>
?????? </property>
?????? <property name="username">
?????????? <value>root</value>
?????? </property>
?????? <property name="password">
?????????? <value>passok</value>
?????? </property>
??? </bean>
?
第二步配置sessionFactory:
?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
?????? <property name="dataSource" ref="myDataSource"></property>
?????? <!-- 定义Hibernate的Session Factory属性 -->
?????? <property name="hibernateProperties">
?????????? <props>
????????????? <prop key="hibernate.show_sql">true</prop>
????????????? <prop key="hibernate.dialect">
????????????????? org.hibernate.dialect.MySQLDialect
????????????? </prop>
?????????? </props>
?????? </property>
?????? <property name="mappingResources">
?????????? <list>
????????????? <!-- 列出所有的映射文件 -->
????????????? <value>com/spring/step1/pojo/Account.hbm.xml</value>
????????????? <value>com/spring/step1/pojo/Loginfo.hbm.xml</value>
?????????? </list>
?????? </property>
??? </bean>
?
第三步;配置txManager
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
??? <property name="sessionFactory" ref="sessionFactory" />
</bean>
第四步定义事务拦截器bean,并注入txManager
<bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
??????? <property name="transactionManager">
??????????? <ref local="txManager" />
??????? </property>
??????? <property name="transactionAttributes">
??????????? <props>
??????????????? <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
??????????????? <prop key="*">PROPAGATION_REQUIRED</prop>
??????????? </props>
??????? </property>
??? </bean>
?
最后定义定义BeanPostProcessorbean
<bean id="autoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
??????? <property name="beanNames">
??????????? <!―把所有需要事务的bean的名字列在下面 -à
??????????? <list>
??????????????? <value>transferService</value>
??????????? </list>
??????? </property>
??????? <property name="interceptorNames">
??????????? <list>
??????????????? <value>txInterceptor</value>
??????????? </list>
??????? </property>
??? </bean>
?
?
?
?
?
?
?
?
?
?
第二种方式:
第零步:添加aspectj这个目录下的两个jar包
第一步改换Application Content 文件头
<?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-2.0.xsd
?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
?????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
?
?
第二步配置datasoure:
<bean id="myDataSource"
?????? class="org.apache.commons.dbcp.BasicDataSource"
?????? destroy-method="close">
?????? <property name="driverClassName">
?????????? <value>com.mysql.jdbc.Driver</value>
?????? </property>
?????? <property name="url">
?????????? <value>jdbc:mysql://localhost:3306/punish</value>
?????? </property>
?????? <property name="username">
?????????? <value>root</value>
?????? </property>
?????? <property name="password">
?????????? <value>passok</value>
?????? </property>
??? </bean>
?
?
?
?
?
?
?
?
?
?
第三步配置sessionFactory:
?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
?????? <property name="dataSource" ref="myDataSource"></property>
?????? <!-- 定义Hibernate的Session Factory属性 -->
?????? <property name="hibernateProperties">
?????????? <props>
????????????? <prop key="hibernate.show_sql">true</prop>
????????????? <prop key="hibernate.dialect">
????????????????? org.hibernate.dialect.MySQLDialect
????????????? </prop>
?????????? </props>
?????? </property>
?????? <property name="mappingResources">
?????????? <list>
????????????? <!-- 列出所有的映射文件 -->
????????????? <value>com/spring/step1/pojo/Account.hbm.xml</value>
????????????? <value>com/spring/step1/pojo/Loginfo.hbm.xml</value>
?????????? </list>
?????? </property>
??? </bean>
?
第四步配置txManager
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
??? <property name="sessionFactory" ref="sessionFactory" />
</bean>
?
?
第五步:
?
?
?
?
?
?
?
?
?
?
<tx:advice id="txAdvice" transaction-manager="txManager">
?????? <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="*" />
?????? </tx:attributes>
</tx:advice>
?
<aop:config>
??? <aop:pointcut id="fooServiceOperation"
expression="execution(* com.spring.step4.dao.service.impl.*.*(..))" />
??? <aop:advisor advice-ref="txAdvice"
?????? pointcut-ref="fooServiceOperation" />
</aop:config>