---
title: "spring常见XML配置aop和事务"
author: "wyyl00"
date: "2010-07-19T19:29:02Z"
tags: []
categories: ["企业架构"]
slug: "spring-xml-aop"
draft: false
lastmod: "2010-07-19T19:29:31Z"
id: 716360
url: "https://aryanwang.iteye.com/blog/716360"
---

XML—AOP配置

```
    <!--业务bean-->   
    <bean id="personService" class="com.aop.service.PersonServiceBean"></bean>
    <!--拦截器bean-->    
    <bean id="myInterceptor" class="com.aop.interceptor.MyInterceptor2"></bean>
    <!-- aop配置 -->
    <aop:config>
      <!-- aspect配置 ，指定myInterceptor-->
      <aop:aspect id="asp" ref="myInterceptor">
        <!-- 配置pointcut，声明对那些方法进行aop -->
        <aop:pointcut id="mypointcut" expression="execution (* com.aop.service.PersonServiceBean.*(..))"/>
        <!-- before拦截，指定pointcut和 before方法-->
        <aop:before pointcut-ref="mypointcut" method="beforeMethod"/>
        <!-- <aop:before pointcut="pointcut表达式" method="beforeMethod"/> -->
        <aop:after pointcut-ref="mypointcut" method="afterMethod" />
      </aop:aspect>
    </aop:config>
```

 

XML-transaction

 

```
<!-- 数据源配置 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="root" />
  </bean>

  <!-- 事务管理器配置 -->
  <bean id="txManage"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!-- 事务管理advice配置 -->
  <tx:advice id="txAdvice" transaction-manager="txManage">
    <tx:attributes>
      <tx:method name="delete" propagation="NEVER" timeout="-1" isolation="DEFAULT" read-only="false"/>
    </tx:attributes>
  </tx:advice>
  <!-- AOP配置，声明对哪些方法进行aop，指定advice为事务管理 -->
  <aop:config>
    <aop:pointcut id="personPointcut"
      expression="execution(* com.jdbc.service.impl.PersonServiceBean.*(..))" />
    <aop:advisor pointcut-ref="personPointcut" advice-ref="txAdvice" />
  </aop:config>
```