Spring AOP 配置 @AspectJ支持的5中通知: —@Before:前置通知在方法执行前执行 —@After:后置通知,在方法执行后执行 —@AfterReturning:返回通知

public interface ArithmeticCalculator {
 
	int add(int i,int j);
	int sub(int i,int j);
	
	int mul(int i,int j);
	int div(int i,int j);
}

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
 
	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}
 
	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}
 
	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}
 
	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

Spring AOP 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">

    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.Impl"/>

    <!--使AspectJ注解起作用:自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.Impl;

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

import java.util.Arrays;
import java.util.List;

/**
 * 把这个类声明为一个切面:
 * 1、需要把该类放入到容器中(就是加上@Component注解)
 * 2、再声明为一个切面(加上@AspectJ注解)
 *
 * @author java
 * @date 2018/6/3 23:20
 */
@Aspect
@Component
public class LoggingAspect {

    //声明该方法为一个前置通知:在目标方法开始之前执行
    //execution中是AspectJ表达式
    //也可@Before(value = "execution(* com.Impl.ArithmeticCalculatorImpl.add(..))")
    @Before(value = "execution(* com.Impl.ArithmeticCalculatorImpl.*(int ,int ))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("beforeMethod "+methodName+" start with "+args);
    }

    //后置通知,就是在目标方法执行之后(无论是否发生异常)执行的通知
    //后置通知中不能访问目标方法的返回结果
    //也可@After(value = "execution(* com.Impl.ArithmeticCalculatorImpl.add(..))")
    @After(value = "execution(* com.Impl.ArithmeticCalculatorImpl.*(int ,int ))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("afterMethod "+methodName+" end with "+args);
    }

    //返回通知,在方法正常结束之后执行的代码
    //返回通知是可以访问到方法的返回值的
    //2. returning 自定义的变量,标识目标方法的返回值自定义变量名必须和通知方法的形参一样
    //也可@AfterReturning(value = "execution(* com.Impl.ArithmeticCalculatorImpl.add(..))")
    @AfterReturning(value = "execution(* com.Impl.ArithmeticCalculatorImpl.*(int ,int ))",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("afterReturning "+methodName+" end with "+result);
    }

    //返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常
    //可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码
    //也可@AfterThrowing(value = "execution(* com.Impl.ArithmeticCalculatorImpl.add(..))")
    @AfterThrowing(value = "execution(* com.Impl.ArithmeticCalculatorImpl.*(int ,int ))",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("afterThrowing "+methodName+" end with "+ ex );
    }

    //环绕通知需要携带ProceedingJoinPoint类型的参数
    //环绕通知类似于动态代理的全过程,这个类型ProceedingJoinPoint的参数可以决定是否执行目标方法
    //且环绕通知必须有返回值,返回值即为目标方法返回值
     //也可@Around(value = "execution(* com.Impl.ArithmeticCalculatorImpl.add(..))")
    @Around(value = "execution(* com.Impl.ArithmeticCalculatorImpl.*(int ,int ))")
    public  void around(ProceedingJoinPoint proceedingJoinPoint){
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        Object[] args = proceedingJoinPoint.getArgs();

        //执行目标方法
        try {
            //前置通知
            System.out.println("beforeMethod "+methodName+" start with "+args);

            result = proceedingJoinPoint.proceed();

            //返回通知
            System.out.println("afterMethod "+methodName+" end with "+result);
        } catch (Throwable throwable) {
            //异常通知
            System.out.println("afterThrowing "+methodName+" exception with "+ throwable );
            throwable.printStackTrace();
        }

        //后置通知
        System.out.println("afterMethod "+methodName+" end with "+args);
        //System.out.println("around "+proceedingJoinPoint);
        return;
    }

}
import com.Impl.ArithmeticCalculator;
import com.Impl.ArithmeticCalculatorImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author chenpeng
 * @date 2018/6/3 23:16
 */
public class aopImplTest {
    public static void main(String[] args) {
        //1、创建SpringIOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("comImpl.xml");
        //2、从IOC容器中国获取Bean的实例
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context.getBean("ArithmeticCalculatorImpl");
        //3、使用Bean
        int result = arithmeticCalculator.add(3,6);
        System.out.println(result);
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值