SpringBoot2.1.0之SpringAOP详解

最近总结SpringBoot中Spring基础,希望可以帮助大家,解决问题。

  1. SpringAOP底层实现原理,上一篇已经总结,遗憾的是只总结了JDK的动态代理,CGLIB动态代理没有(后期一定补上)
  2. Spring利用动态代理带给我们便捷:(一个切面基本描述所有常用AOP技术,有不足必虚心接受指点*-*)
package com.rhb.aspect;

import java.lang.reflect.Method;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.rhb.annotation.Log;
import com.rhb.pojo.User;
import com.rhb.service.ValidateService;
import com.rhb.service.impl.ValidateServiceImpl;

/**
 * @author renhuibo  2019-08-21 23:36:04
 * @Description
 */
@Aspect
@Component
public class DefaultAspect {

	@DeclareParents(value="com.rhb.service.impl.HelloServiceImpl",defaultImpl=ValidateServiceImpl.class)
	public ValidateService validate;
	
	@Pointcut("execution(* com.rhb.service.impl.HelloServiceImpl.makeUser(..))")
	public void pointCut() {
		
	}
	
	@Before("pointCut() && args(user,throwException)")
	public  void before(JoinPoint joinPoint,User user,boolean throwException) {
		System.out.println("\nbefore...");
		System.out.println(user);
	}
	
	@After("pointCut()")
	public void after(JoinPoint joinPoint) {
		System.out.println("\nafter...");
	}

	/**
	 * @author renhuibo  2019年8月24日
	 * @Description : 如果proceed()
	 */
	@Around("pointCut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		System.out.println("\naround before...");
		Object obj = proceedingJoinPoint.proceed();
		System.out.println("around end...");
		return obj;
	}
	
	@AfterThrowing(pointcut="pointCut()",throwing="exception")
	public void afterThrowing(Throwable exception) {
		System.out.println("\naftertThrowing...s");
		System.out.println("Exception: "+exception);
	}
	
	@SuppressWarnings("unchecked")
	@AfterReturning(pointcut="pointCut()",returning="returnValue")
	public void afterReturning(JoinPoint joinPoint,User returnValue) {
		System.out.println("\nafterReturning...");
		
		Signature signature = joinPoint.getSignature();
		
		//连接点方法名称
		String methodName = signature.getName();
		System.out.println("切点方法: "+methodName);
		
		//连接点方法参数
		Object[] args = joinPoint.getArgs();
		System.out.println("切点参数: "+Arrays.toString(args));
		
		//链接点所属对象
		Object obj = joinPoint.getTarget();
		Class cla = obj.getClass();
		System.out.println("切点所属类: "+cla.getName());
		
		//获取连接点方法对象
		Class targetClass = obj.getClass();
		Method method = null;
		Method[] methods = targetClass.getMethods();
		for(Method m : methods) {
			if(m.getName().equals(methodName)){
				method = m;
				break;
			}
		}
		
		//连接点方法注解(对象注解同理)
		if(method.isAnnotationPresent(Log.class)) {
			Log log = method.getAnnotation(Log.class);
			System.out.println("切点方法注解值: "+log.value());
		}
		
		if(cla.isAnnotationPresent(Log.class)) {
			Log log = (Log) cla.getAnnotation(Log.class);
			System.out.println("切点所属类注解值: "+log.value());
		}
		
		System.out.println("返回值: "+returnValue);
	}
}
简单阐述切面的功能和遇到的问题点:(所有的总结,偏向使用,基础还希望大家基本了解)

功能点:
    a. 所有通知类型使用: @Before、@After、@Around、@AfterReturning、@AfterThrowing
    b. 增强处理注解@DeclareParents
    c. 连接点对象JoinPoint,以及子接口ProceedJoinPoint。
    d. 连接点所在类中相关组成的获取: 目标类、目标方法、目标方法参数、目标类|方法注解获取、返回数据、运行中抛出的异常。

易错点:
    a. @Around中使用ProceedJoinPoint.proceed()后,记得将返回值返回。不返回的话,在@AfterReturning通知中将获取不到返回值,而且@Before通知也将不被调用(这里原因我猜想是InvocationHandler实现类中,定义的通知执行流程有关,因为在实际开发环境中这种使用proceed()方法又不返回的情况,很少遇到,就偷个小懒不看源码了)

    b. 在使用@DeclareParent的value属性中,将原来的"...HelloServiceImpl+"改为去掉“+”号,否则会报异常:warning can't determine implemented interfaces of missing type...(不知道是SpringBoot引用Spring的新设置,还是Bug,先接受用喽。)

    c. 使用AspectJ编程的用args(Object ...)获取参数的时候,要知道args本身主要用来匹配连接点。


    

  3.  相关概念:连接点-joinpoint、切点-pointcut、切面-aspect、通知-advice、目标对象-target、引入-introduction、织入-weaving,以及AspectJ关于AOP切点指示器,大家自行补上喽!*-*

                                            图-AspectJ关于AOP切点指示器

github源码地址:https://github.com/18835572909/instance-aop-code.git

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值