配置文件中我们需要加一个tx命名空间,同时配置数据库链接、事务通知和AOP。 AOP的作用就是将事务通知加入到匹配的类与方法中,当调用匹配的类和方法的时候就会触发事务
<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="jee.pk2"></context:component-scan> <!-- 引入jdbc.properties --> <context:property-placeholder location="jdbc.properties"/> <!-- 配置dbcp数据库 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${uName}" /> <property name="password" value="${password}" /> <property name="initialSize" value="3" /> </bean> <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置一个事物通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 以get开头的所有方法都使用只读事务,只读事务可以提高查询效率 --> <tx:method name="get*" read-only="true" /> <!-- 其他方法是普通事务 --> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="userServiceOperation" expression="execution(* jee.pk2.UserService.*(..))" /> <!-- 通知器,通知和切入点 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/> </aop:config> </beans>加入事务的类
package jee.pk2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; @Component("userService") public class UserServiceImpl implements UserService { private UserDao userDao; @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void remove() { userDao.delete(3); if(1==1) { throw new RuntimeException("某个错误"); } userDao.delete(2); } }主方法:
package jee.pk2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("beans_pk2.xml"); UserService userService=ac.getBean("userService", UserService.class); userService.remove(); } }