SpringAOP-xml配置AOP-xml+注解配置AOp

2021-02-12 11:20

阅读:344

标签:ack   join   前置   mic   cut   exe   row   throwable   new   

实体类如下

技术图片

Calculation是我们用的类,PointCut是我们要实现通知的切面类

Calculation类如下

package com.llf.aop;

import org.springframework.stereotype.Component;

/**
 * @author linglongfang
 */
@Component
public class Calculation {

    public int add(int i,int j){
        System.out.println("执行add");
        return i+j;
    }

    public int sub(int i,int j){
        System.out.println("执行sub");
        return i-j;
    }

    public int mul(int i,int j){
        System.out.println("执行mul");
        return i*j;
    }

    public int div(int i,int j){
        System.out.println("执行div");
        return i/j;
    }
}

 

PointCut类如下

package com.llf.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author linglongfang
 */
@Order(1)
@Aspect
@Component
public class PointCut {
   //切点
    @Pointcut("execution(* com.llf.aop.Calculation.*(int, int))")
    public void defaultPointCut(){}


    //前置通知
    @Before("defaultPointCut()")
    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("执行方法前:"+methodName+"参数为"+args);
    }

    //后置通知  后置通知拿不到返回值
    @After("defaultPointCut()")
    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法后:"+methodName);
    }

    //返回通知
    @AfterReturning(value="defaultPointCut()",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法返回后:"+methodName+":结果是"+result);
    }

    //异常通知
    @AfterThrowing(value="defaultPointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法异常后:"+methodName+":抛出"+ex);
    }

      //环绕通知
//    @Around("defaultPointCut()")
//    public Object aroud(ProceedingJoinPoint pjd){
//        Object result = null;
//        try {
//            System.out.println("前置通知");
//            result = pjd.proceed();
//            System.out.println("返回通知");
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//            System.out.println("异常通知");
//        }
//        System.out.println("后置通知");
//        return result;
//    }

}

 

 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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    context:component-scan base-package="com.llf.aop">context:component-scan>
    
    aop:aspectj-autoproxy>aop:aspectj-autoproxy>

 beans>

 

测试类Main:

package com.llf.aop;

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

/**
 * @author linglongfang
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new  ClassPathXmlApplicationContext("aop.xml");
        Calculation calculation = (Calculation) ctx.getBean("calculation");
        calculation.add(1, 3);
    }
}

 

测试结果:

技术图片

 

SpringAOP-xml配置AOP-xml+注解配置AOp

标签:ack   join   前置   mic   cut   exe   row   throwable   new   

原文地址:https://www.cnblogs.com/linglongfang/p/12731192.html


评论


亲,登录后才可以留言!