Python 官方文档:入门教程 => 点击学习
目录aop事务管理<aop:advisor>两种配置方式方式一方式二hibernate事务配置Aop aop:advisor模式AOP事务管理<aop:adviso
@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>
<!-- 使用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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: AOP之事务管理<aop:advisor>的两种配置方式
本文链接: https://www.lsjlt.com/news/158184.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0