7、基于XML的AOP(了解)


【尚硅谷】SSM框架全套教程-讲师:杨博超

但行好事,莫问前程

7、基于XML的AOP(了解)

7.1、准备工作

1 添加依赖

<!-- spring-aspects会帮我们传递过来aspectjweaver -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.22</version>
</dependency>

2 目标资源

接口

public interface Calculator {
    int add(int i, int j);

    int sub(int i, int j);

    int mul(int i, int j);

    int div(int i, int j);

}

实现类

@Component
public class CalculatorPureImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("方法内部result=" + result);
        return result;
    }

    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("方法内部result=" + result);
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("方法内部result=" + result);
        return result;
    }

    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("方法内部result=" + result);
        return result;
    }
}

7.2、实现

1 LogAspect 切面类

@Component // @Component注解保证这个切面类能够放入IOC容器
public class LogAspect {

    public void beforeMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        String s = Arrays.toString(joinPoint.getArgs());
        System.out.println("Logger-->前置通知,方法名:" + name + ",参数:" + s);
    }

    public void afterMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->后置通知,方法名:" + name);
    }

    public void afterReturningMethod(JoinPoint joinPoint, Object result) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->返回通知,方法名:" + name + "结果:" + result);
    }

    public void afterThrowingMehtod(JoinPoint joinPoint, Throwable ex) {
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger-->异常通知,方法名:" + name + "异常:" + ex);
    }

    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        Object result = null;
        try {
            System.out.println("环绕通知-->目标对象方法执行之前");
            //目标对象(连接点)方法的执行
            result = joinPoint.proceed();
            System.out.println("环绕通知-->目标对象方法返回值之后");
        } catch (Throwable e) {
            System.out.println("环绕通知-->目标对象方法出现异常时");
            throw new RuntimeException(e);
        } finally {
            System.out.println("环绕通知-->目标对象方法执行完毕");
        }
        return result;
    }

}

2 ValidateAspect 切面类

@Component
public class ValidateAspect {

    public void beforeMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        String s = Arrays.toString(joinPoint.getArgs());
        System.out.println("Validate-->前置通知,方法名:" + name + ",参数:" + s);
    }
}

3 配置文件

<context:component-scan base-package="pers.tianyu.proxyxml">
</context:component-scan>

<aop:config>
    <!--设置一个公共的切入点表达式-->
    <aop:pointcut id="pointCut" expression="execution(public int pers.tianyu.proxyxml.Calculator.*(..))"/>
    <!--将IOC容器中的某个bean设置切面 -->
    <aop:aspect ref="logAspect">
        <aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before>
        <aop:after method="afterMethod" pointcut-ref="pointCut"></aop:after>
        <aop:after-returning method="afterReturningMethod" returning="result"
                             pointcut-ref="pointCut"></aop:after-returning>
        <aop:after-throwing method="afterThrowingMehtod" throwing="ex" pointcut-ref="pointCut"></aop:after-throwing>
        <aop:around method="aroundMethod" pointcut-ref="pointCut"></aop:around>
    </aop:aspect>
    <!--将validateAspect设置切切面,order设置优先级-->
    <aop:aspect ref="validateAspect" order="1">
        <aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before>
    </aop:aspect>
</aop:config>

4 测试

@Test
public void test() {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
    // 使用接口类型
    Calculator bean = ioc.getBean(Calculator.class);
    bean.add(1, 1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值