AOP主要注解:

@Aspect,作用在类上,说明这是一个Aspect切面类。

@Pointcut,用来描述,你需要在哪些类的哪些方法中植入你的代码。

@Adive,与Pointcut配合使用,主要说明在Pointcut标记方法的什么时机执行,执行之前?执行之后?

@Pointcut express(切面表达式)

designators指示器:

匹配方法:execution() 通过什么方式去匹配哪些类的哪些方法(重点掌握)。

匹配注解:@annotation() @args() @within() @target()

匹配包/类型:within()

匹配对象:this() bean() target()

匹配参数:args()

wildcards通配符:

 *  匹配任意数量的字符
.. 匹配指定类及其子类
+ 匹配任意数的子包或参数

operators运算符:&&与 ||或 !非

5中Advice

@Before

@After

@AfterReturning

@AfterThrowing

@Around

示例代码

@Aspect
@Component
public class HttpAspect {
private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
public void log(){
} /**
* 在截取的方法前执行
* @param joinPoint
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); // url
logger.info("url={}", request.getRequestURL());
// method
logger.info("method={}", request.getMethod());
// ip
logger.info("ip={}", request.getRemoteAddr());
// 类方法
logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
// 参数
logger.info("args={}", joinPoint.getArgs());
} /**
* 获取方法返回参数
* @param object
*/
@AfterReturning(returning = "object", pointcut = "log()")
public void doAfterReturning(Object object){
logger.info("response={}", object);
}
}

Advice示例

@Aspect
@Component
public class AdviceAspectConfig { /********PointCut********/ @Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
public void matchAnnotation(){} @Pointcut("execution(* com.vmware.AopExecutionDemo.service.*.find*(int))")
public void matchIntArgs(){} @Pointcut("execution(public * com.vmware.AopExecutionDemo.service..*.*(..) throws java.lang.IllegalAccessException)")
public void matchExecption(){} @Pointcut("execution(int com.vmware.AopExecutionDemo.service.*.*(..))")
public void matchReturn(){} /********Advice********/ /**
* 获取方法输入的参数
* @param productId
*/
@Before("matchIntArgs() && args(productId)")
public void before(int productId){
System.out.println("### before ### productId = [" + productId + "]");
} /**
* 获取方法返回参数
* @param result
*/
@AfterReturning(value = "matchReturn()", returning = "result")
public void after(int result){
System.out.println("### after ###:" + result);
} /**
* Before + After + AfterReturning
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("matchReturn()")
public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("### before ###");
Object result;
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("### after ###" + result);
return result;
}
}

@annotation示例

@Aspect
@Component
public class AnnotationAspectConfig { /**
* 匹配带有 AdminOnlyMethod Annotation的方法
*/
@Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
public void matchAnnotation(){} @Before("matchAnnotation()")
public void before(){
System.out.println("###matchAnnotation && Before");
}
}
@Aspect
@Component
public class AnnotationClassAspectConfig { /**
* 匹配带有 AdminOnlyClass Annotation的类
*/
@Pointcut("@within(com.vmware.AopExecutionDemo.annotation.AdminOnlyClass)")
public void annotationClassCondition(){} @Before("annotationClassCondition()")
public void before(){
System.out.println("###annotationClass && before");
}
}

SpringBoot AOP示例的更多相关文章

  1. springBoot AOP学习(一)

    AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...

  2. 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~

    Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...

  3. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  4. springboot+aop切点记录请求和响应信息

    本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...

  5. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  6. springboot aop 不生效原因解决

    最近参照资料创建Springboot AOP ,结果运行后aop死活不生效. 查明原因: 是我在创建AOP类时选择了Aspect类型,创建后才把这个文件改为Class类型,导致一直不生效, 代码配置这 ...

  7. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  8. springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)

    https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...

  9. dubbo+zookeeper+springboot简单示例

    目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...

随机推荐

  1. Vue入门之旅:一报错 Unknown ... make sure to provide the "name" option及error compiling template

    报错一: Unknown custom element: <custom-select> - did you register the component correctly? For r ...

  2. The OpenCV Coding Style Guide

    https://github.com/opencv/opencv/wiki/Coding_Style_Guide

  3. office 2013 activiate---(run as admin)

    win7 office 2013 activiate---(run as admin) empty the garbage in osx rm -rf ~/.Trash

  4. iOS之block,一点小心得

    作为一个iOS开发程序员,没用过block是不可能的.这次我探讨的是block原理,但是有些更深层次的东西,我也不是很清楚,以后随着更加了解block将会慢慢完善. 第一个问题,什么是block? 我 ...

  5. <2014 05 09> 程序员:从C++转到Java需注意的地方

    最近想玩玩Android的APP开发,从C++角度来学习Java.Java可以说是一个优化精简版的C++,去除了底层C的很多特性.找了这篇文章. --------------------------- ...

  6. python的socket的学习

    一.Socket相关知识 1.socket是什么: socket是应用层与TCP/IP协议族通信的中间软件抽象层,他是一组接口.在设计模式中,Socket其实就是一个门面模式. 它把复杂的TCP/IP ...

  7. Java 集合框架查阅技巧

    如何记录每一个容器的结构和所属体系呢? List ArrayList LinkedList Set HashSet TreeSet 其中,后缀名就是该集合所属的体系,前缀名就是该集合的数据结构. 看到 ...

  8. delphi inifile 支持 utf8

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. linux下Pl353 NAND Flash驱动分析

    linux的NAND Flash驱动位于drivers/mtd/nand子文件夹下: nand_base.c-->定义通用的nand flash基本操作函数,如读写page,可自己重写这些函数 ...

  10. HDU3552(贪心)

    题目是将一系列点对(a,b)分成两个集合.使得A集合的最大a+B集合的最大数b得和最小. 思路:http://blog.csdn.net/dgq8211/article/details/7748078 ...