声明式事务处理:
* 程序员声明
* sping容器来处理事务
applicationContext.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:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置aop: × 目标类 × 通知 × aop配置 --> <bean id="personService" class="jdbc.transaction.xml.PersonServiceImpl"> <property name="personDao" ref="personDao" /> </bean> <bean id="personDao" class="jdbc.transaction.xml.PersonDaoImpl"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务管理器:告诉spring容器用哪种技术进行事物管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置通知 --> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <!-- save*以save开头的方法 * 除上述方法外的所有方法 isolation:隔离机制 propagation: 事务传播机制 --> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <aop:pointcut expression="execution(* jdbc.transaction.xml.PersonServiceImpl.*(..))" id="perform"/> <aop:advisor advice-ref="tx" pointcut-ref="perform"/> </aop:config> <!-- 配置数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3399/db" /> <property name="username" value="root" /> <property name="password" value="3333456" /> </bean> </beans>
浙公网安备 33010602011771号