AOP之事务管理aop:advisor的两种配置方式 目录 AOP事务管理aop:advisor两种配置方式 方式一 方式二 hibernate事务配置Aop aop:advisor模式 AOP事务管理aop:advisor两种配置方式 方式一 @transactionManagerbean.xml ?xml version=1.0 encoding=UTF-8? beans xmlns=http://www.springframew ...
目录
- AOP事务管理<aop:advisor>两种配置方式
- 方式一
- 方式二
- hibernate事务配置Aop aop:advisor模式
AOP事务管理<aop:advisor>两种配置方式
方式一
@transactionManagerbean.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.wanho.java150"/>
<context:property-placeholder location="classpath*:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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 提供 jdbc 帮助类-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--基于@Transactional-->
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven />
</beans>
ServiceImpl
在service实现类的最外层或者具体方法上添加@Transactional注解
@Service
//@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDAO customerDAO ;
@Autowired
private LoginLogDAO loginLogDAO ;
@Transactional
@Override
public Customer login(String account, String pswd) {
return null;
}
}
方式二
bean.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.wanho.java150"/>
<context:property-placeholder location="classpath*:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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 提供 jdbc 帮助类-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--jdbc 事务管理配置基于xml-->
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务隔离级别-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--还可以添加回滚、只读等标签配置-->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--业务层 使用 事务切面-->
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* com.wanho.java150.service.impl.CustomerServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
hibernate事务配置Aop aop:advisor模式
<!-- 使用HibernateTransactionManager管理hibernate事务 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建事务规则 -->
<!-- 表示我们要控制事务的地方,如果方法名开头是add、update和delete,那么使用REQUIRED事务传播方式。那么其他的方法使用REQUIRED事务传播方式,并且是只读 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 告知事务的切入点 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tiema..service..*.*(..))" />
</aop:config>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
沃梦达教程
本文标题为:AOP之事务管理<aop:advisor>的两种配置方式
猜你喜欢
- JSP页面间传值问题实例简析 2023-08-03
- Java实现顺序表的操作详解 2023-05-19
- 深入了解Spring的事务传播机制 2023-06-02
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- Java中的日期时间处理及格式化处理 2023-04-18
- JSP 制作验证码的实例详解 2023-07-30
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- Spring Security权限想要细化到按钮实现示例 2023-03-07