亲宝软件园·资讯

展开

AOP事务管理<aop:advisor>配置

Simon_liu94 人气:0

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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论