首先,新建一个需要切面拦截的方法,再这个方法执行前以及执行后我们需要无耦合的添加一些提前后滞后的处理。

package com.fpga.java.api.impl;@Component
@Service(timeout = 5000)
public class FPGAComputeServiceImpl implements FPGAComputeService{@Overridepublic Map<String, String> send(int uid, String chipId,int chipType, String data) {……}/*** @param uid 用户ID* @param chipType 芯片类型*/public Map<String, String> result(int uid, int chipType) {……}
}

然后新建一个切面类:

package com.fpga.java.api.aop;
import java.lang.reflect.Modifier;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class FPGAComputeServiceAop {@Before(value = "execution(* com.fpga.java.api.impl.FPGAComputeServiceImpl.*(..))")public void beforeAdvice(JoinPoint joinPoint) {System.out.println("目标方法名为:" + joinPoint.getSignature().getName());System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//获取传入目标方法的参数Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "个参数为:" + args[i]);}System.out.println("被代理的对象:" + joinPoint.getTarget());System.out.println("代理对象自己:" + joinPoint.getThis());}@After(value = "execution(* com.fpga.java.api.impl.FPGAComputeServiceImpl.*(..))")public void afterAdvice(JoinPoint joinPoint) {System.out.println("目标方法名为:" + joinPoint.getSignature().getName());System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//获取传入目标方法的参数Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "个参数为:" + args[i]);}System.out.println("被代理的对象:" + joinPoint.getTarget());System.out.println("代理对象自己:" + joinPoint.getThis());}
}

随意测试下

结果:

目标方法名为:send
目标方法所属类的简单类名:FPGAComputeServiceImpl
目标方法所属类的类名:com.fpga.java.api.impl.FPGAComputeServiceImpl
目标方法声明类型:public
第1个参数为:0
第2个参数为:111
第3个参数为:0
第4个参数为:222
被代理的对象:com.fpga.java.api.impl.FPGAComputeServiceImpl@607dbd4f
代理对象自己:com.fpga.java.api.impl.FPGAComputeServiceImpl@607dbd4f
目标方法名为:send
目标方法所属类的简单类名:FPGAComputeServiceImpl
目标方法所属类的类名:com.fpga.java.api.impl.FPGAComputeServiceImpl
目标方法声明类型:public
第1个参数为:0
第2个参数为:111
第3个参数为:0
第4个参数为:222
被代理的对象:com.fpga.java.api.impl.FPGAComputeServiceImpl@607dbd4f
代理对象自己:com.fpga.java.api.impl.FPGAComputeServiceImpl@607dbd4f

@Aspect:作用是把当前类标识为一个切面供容器读取

exection表达式基本语法格式为:
执行(<修饰符模式>?<返回类型模式> <方法名模式>(<参数模式>)<异常模式>?)除了返回类型模式,方法名模式和参数模式外,其它项都是可选的。

execution(public * ArithmeticCalculator.*(…))
含义:ArithmeticCalculator接口的所有公有方法

execution(public double ArithmeticCalculator.*(…))
含义ArithmeticCalculator接口中返回double类型数值的方法

execution(public double ArithmeticCalculator.*(double, …))
含义第一个参数为double类型的方法。
“…” 匹配任意数量、任意类型的参数。

execution(public double ArithmeticCalculator.*(double, double))
含义参数类型为double,double类型的方法

切入点表达式可以通过 “&&”、“||”、“!”等操作符结合起来。

execution (* .add(int,…)) || execution( *.sub(int,…))
含义任意类中第一个参数为int类型的add方法或sub方法

切片注解:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行 。
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行

springboot 注解@Before和@After的用法相关推荐

  1. springboot+mybatis集成自定义缓存ehcache用法笔记

    今天小编给大家整理了springboot+mybatis集成自定义缓存ehcache用法笔记,希望对大家能有所办帮助! 一.ehcache介绍 EhCache 是一个纯Java的进程内缓存管理框架,属 ...

  2. 十、springboot注解式AOP(@Aspect)统一日志管理

    springboot注解式AOP(@Aspect)统一日志管理 简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...

  3. SpringBoot注解大全(转)

    原文链接:[springBoot系列]--springBoot注解大全 一.注解(annotations)列表  @SpringBootApplication:包含了@ComponentScan.@C ...

  4. spring-boot注解详解(三)

    1.SpringBoot/spring @SpringBootApplication: 包含@Configuration.@EnableAutoConfiguration.@ComponentScan ...

  5. spring-boot注解详解(一)

    spring-boot注解详解(一) @SpringBootApplication @SpringBootApplication = (默认属性)@Configuration + @EnableAut ...

  6. @configuration注解_超级全面的 SpringBoot 注解介绍,每一个用途都应该清晰

    一.注解(annotations)列表 1.@SpringBootApplication 包含了@ComponentScan.@Configuration和@EnableAutoConfigurati ...

  7. 【SpringBoot 】SpringBoot注解详解

    [SpringBoot ]SpringBoot注解详解 一.注解(annotations)列表  @SpringBootApplication:包含了@ComponentScan.@Configura ...

  8. SpringBoot : 注解@Resource

    1.美图 2.概述 在项目开发中,@Autowired和@Resource之争,一直搞不清楚,反正也不想搞清楚到底什么时候用@Autowired,什么场景下用@Resource,就一直用@Autowi ...

  9. SpringBoot注解把配置文件自动映射到属性和实体类实战

    SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...

最新文章

  1. HDU2544 最短路(模版题dijkstra/floyd/spfa)
  2. oracle-ORA-01555错误
  3. 计算机网络时延图,计算机网络中网站性能延迟加载图像的示例分析
  4. 基于注解进行bean的装配
  5. Android 系统权限
  6. 简单的多线程实例下载(供初学者下载学习)
  7. org.apache.ibatis.binding.BindingException: Parameter '1' not found. Available parameters are [arg3,
  8. 饿了么想解决外卖小哥马路杀手的问题,但用无人机?
  9. Android约束布局
  10. Tecplot读取Excel文件中的数据
  11. 蔡义江《红楼梦诗词曲赋评注》下
  12. ios相机黑边_iOS照相机去黑框
  13. 关于Qt bindValue函数出错问题
  14. linux屏幕伽马值设置,设置计算机显示屏的亮度和对比度伽玛值
  15. DHCP Snooping IPSG
  16. 国外量化平台,以QuantOpian为例
  17. 集线器,路由器,交换机之间的区别
  18. 高速公路数字孪生3D场景制作全流程记录【Blender + UE4】
  19. 插入 PNG 图片至 Excel
  20. 嘿~你想写出五彩斑斓的BUG吗

热门文章

  1. CSS常用中文字体的英文名称对照表
  2. 智慧养殖系统应用现状
  3. 如何将电脑的歌曲传到iphone手机
  4. 腾讯又一重磅服务关闭!网友:9年了,还是没了……
  5. 这个世界只有原子和虚空
  6. 利用POI生成EXCEL报表(通过web页面导出后台数据)
  7. React内联方式使用backgroundImage
  8. 浏览器环境下JavaScript脚本加载与执行探析之defer与async特性
  9. windows XP(32位) 安装java web 部署环境
  10. 索尼手机刷twrp教程