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>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: