Spring AOP的xml配置

创建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"
       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">

    <!--创建被增强的对象-->
    <bean id="book" class="xin.students.aopxml.Book"/>

    <!--创建增强的对象-->
    <bean id="bookProxy" class="xin.students.aopxml.BookProxy"/>

    <!--配置aop增强-->
    <aop:config>
        <!--创建切入点表达式,并起一个名称-->
        <aop:pointcut id="myExecution" expression="execution(* xin.students.aopxml.Book.buy(..))"/>

        <!--配置切面,ref后是增强类的bean值-->
        <aop:aspect ref="bookProxy">
            <!--增强类用在具体的方法上,第一个参数是增强类的方法名称,第二个类是引用上面的execution表达式-->
            <!--不用pointcut-ref,也可以用pointcut="execution(...)"-->
            <!--这五条分别是,前置通知,后置通知,异常通知,最终通知,环绕通知-->
            <aop:before method="before" pointcut-ref="myExecution"/>
            <aop:after-returning method="afterReturning" pointcut-ref="myExecution"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="myExecution"/>
            <aop:after method="after" pointcut-ref="myExecution"/>
            <aop:around method="around" pointcut-ref="myExecution"/>
        </aop:aspect>
    </aop:config>

</beans>

创建增强类

package xin.students.aopxml;

//被增强的类
public class Book {
    public void buy() {
        System.out.println("buy");
    }
}

创建被增强类

package xin.students.aopxml;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

public class BookProxy {
    public void before() {
        System.out.println("before.....................");
    }

    //后置通知,方法返回结果后执行
    public void afterReturning() {
        System.out.println("afterReturning.....................");
    }

    //最终通知,方法结束后通知
    public void after() {
        System.out.println("after.....................");
    }

    //异常通知
    public void afterThrowing() {
        System.out.println("afterThrowing.....................");
    }

    //环绕通知
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前通知.....................");
        proceedingJoinPoint.proceed();   //执行被增强的方法

        //当发生异常时不会执行下面的代码和后置通知,只会执行最终通知
        System.out.println("环绕后通知.....................");
        return null;
    }
}

创建测试方法

package xin.students.aopxml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("aopxml.xml");
        Book book = ac.getBean("book", Book.class);
        book.buy();
    }
}

运行结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值