AOP理解为AOP面向切面编程,就是对原有业务方法进行扩展,在原有代码发生变化时抽取公共方法。组合方面(Aspect):一个对象(提供扩展方法的对象)。切入点(PointCut):切面中方法执行的地址。目标(Target):被切入的物体。建议:扩展方法的方法。JoinPoint:切入对象的切入匹配点。例如:厨师需要做饭,因为吃饭的人多的时候,需要有人帮他递菜。这个路人就是面条。他相当于横向切入了我们原来的业务。递菜这个动作相当于一个切入点。传菜器相当于增强了烹饪前后的功能。传菜者需要在做菜前后做动作(前:报菜名,后:传菜)。这是之前和之后的建议。连接点其实就相当于在原来业务上的工作。实现方式使用原生AOP接口配置:java://根据方法调用的不同建议不同,此时为public类LogimplementsMethodBeforeAdvice{/****@parammethod方法*@paramargs参数*@paramtarget目标对象*@throwsThrowable*/@Overridepublicvoidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable{System.out.println(target.getClass().getName()+"of"+method.getName());}}//方法返回后publicclassAfterReturnLogimplementsAfterReturningAdvice{@OverridepublicvoidafterReturning(ObjectreturnValue,方法method,Object[]args,Objecttarget)throwsThrowable{System.out.println(target.getClass().getName()+"of"+method.getName()+"结果为"+returnValue);}}测试类:publicclassTest{publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");BookServicebookService=(BookService)context.getBean("bookService");bookService.add();}}使用自定义类实现注解方法配置:java:@AspectpublicclassAspectLog1{@Before("执行(*com.spring.service.BookServiceImpl.*(..))")publicvoidbefore1(){System.out.println("运行前1");}@Before("execution(*com.spring.service.BookServiceImpl.*(..))")publicvoidbefore2(){System.out.println("运行前2");}@After("execution(*com.spring.service.BookServiceImpl.*(..))")publicvoidafter(){System.out.println("运行后");}@AfterReturning("execution(*com.spring.service.BookServiceImpl.*(..))")publicvoidafterReturning(){System.out.println("返回结果后");}@Around("execution(*com.spring.service.BookServiceImpl.*(..))")publicObjectaround(ProceedingJoinPointpj)throwsThrowable{System.out.println("环绕前");对象结果=pj.proceed();System.out.println("环绕后");返回结果;}}AOP不同advice执行顺序around->before->方法正常执行->around(end)->after->afterReturningaround->before->方法异常执行->around(end)->after->afterThrowing->throwingexception如果是同一个aspect相同的advice,建议写在一个PointCut中或者可以分成两个不同aspect相同advice的Aspect,使用@Order注解判断顺序,以及值越小,优先级越高。不同的通知按照上面的顺序执行。