Spring AOP:xml和注解两种配置实例

一、xml形式
1.1、切面类

package aop;

import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component //注解形式生产bean无需xml配置
public class XmlAspect {
    public void doBefore(){
        System.out.println("前置通知");
    }

    //声明后置通知
    public void doAfterReturning(String result){
        System.out.println("后置通知");
        System.out.println("---" + result + "---");
    }

    //声明异常通知
    public void doAfterThrowing(Exception e){
        System.out.println("异常通知");
        System.out.println(e.getMessage());
    }

    public void doAfter(){
        System.out.println("最终通知");
    }

    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        Gson gson = new Gson();
        System.out.println("代理信息:被代理对象:"+ pjp.getTarget() + ",代理方法:"+ (MethodSignature)pjp.getSignature() +",方法参数:" + gson.toJson(pjp.getArgs()));
        System.out.println("进入方法---环绕通知");
        Object o = pjp.proceed();
        System.out.println("退出方法---环绕通知");
        return o;
    }
}

1.2.目标对象:AServiceImpl

package aop;

import org.springframework.stereotype.Component;

@Component //注解形式生产bean无需xml配置
public class AServiceImpl implements AService {
    @Override
    public void barA(){
        System.out.println("AServiceImpl.barA()");
    }

    @Override
    public void fooA(String msg){
        System.out.println("AServiceImpl.fooA(msg:" + msg + ")");
    }
}

1.3.目标对象:BServiceImpl

package aop;

import org.springframework.stereotype.Component;

@Component //注解形式生产bean无需xml配置
public class BServiceImpl {
    public void barB(String msg, int type){
        System.out.println("BServiceImpl.barB(msg:"+msg+"type:"+type+")");
        if (type == 1){
            throw new IllegalArgumentException("测试异常");
        }
    }

    public void fooB(){
        System.out.println("BServiceImpl.fooB()");
    }
}

1.4.applicationContext:Spring xml配置




    
    
    
    
    
        
            
            
        
    

1.5.测试类

import aop.AService;
import aop.BServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {
    @Test
    public void aopAnnonationTest(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        AService aService = (AService) ac.getBean("AServiceImpl");
        aService.fooA("test");
        BServiceImpl bService = (BServiceImpl)ac.getBean("BServiceImpl");
        bService.barB("message info", 2);
    }
}

1.6.输出结果


image.png

二、注解形式
2.1切面类

package aop;

import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component //注解形式生产bean无需xml配置
public class AnnotationAspect {
    @Pointcut("execution(* aop.*.*(..))")
    private void pointCutMethod(){
    }
    //声明前置通知
    @Before("pointCutMethod()")
    public void doBefore(){
        System.out.println("前置通知");
    }

    //声明后置通知
    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")
    public void doAfterReturning(String result){
        System.out.println("后置通知");
        System.out.println("---" + result + "---");
    }

    //声明异常通知
    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
    public void doAfterThrowing(Exception e){
        System.out.println("异常通知");
        System.out.println(e.getMessage());
    }

    @After("pointCutMethod()")
    public void doAfter(){
        System.out.println("最终通知");
    }

    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        Gson gson = new Gson();
        System.out.println("代理信息:被代理对象:"+ pjp.getTarget() + ",代理方法:"+ (MethodSignature)pjp.getSignature() +",方法参数:" + gson.toJson(pjp.getArgs()));
        System.out.println("进入方法---环绕通知");
        Object o = pjp.proceed();
        System.out.println("退出方法---环绕通知");
        return o;
    }
}

2.2 applicationContext:Spring xml配置




    
    
    
    
    

目标类同1.3、1.4,测试类同1.5

2.3 输出结果


image.png

最后,再拓展提醒下:
任何通知(Advice)方法可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型。JoinPoint接口提供了一系列有用的方法, 比如 getArgs() (返回方法参数)、getThis() (返回代理对象)、getTarget() (返回目标)、getSignature() (返回正在被通知的方法相关信息)和 toString() (打印出正在被通知的方法的有用信息。

其中getSignature()返回的Signature对象可强制转换为MethodSignature,其功能非常强大,能获取包括参数名称在内的一切方法信息。

附pom依赖(Spring各组件版本尽量保持一致,不然由于版本不一致有可能会报错):

    
    
      org.springframework
      spring-core
      4.3.8.RELEASE
    
    
    
      org.springframework
      spring-context
      4.3.8.RELEASE
    
    
    
      org.springframework
      spring-beans
      4.3.8.RELEASE
    
    
    
      org.springframework
      spring-web
      4.3.8.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.3.8.RELEASE
    
    
    
      org.aspectj
      aspectjtools
      1.8.9
    
    
      org.aspectj
      aspectjweaver
      1.9.3
    

拓展练习:
AOP注解的方式校验参数:https://blog.csdn.net/qq496013218/article/details/78595572
AOP实现机制:https://www.iteye.com/topic/1116696

你可能感兴趣的:(Spring AOP:xml和注解两种配置实例)