SpringBoot AOP示例
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示例的更多相关文章
- springBoot AOP学习(一)
AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...
- 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~
Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...
- SpringBoot+AOP整合
SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...
- springboot aop 不生效原因解决
最近参照资料创建Springboot AOP ,结果运行后aop死活不生效. 查明原因: 是我在创建AOP类时选择了Aspect类型,创建后才把这个文件改为Class类型,导致一直不生效, 代码配置这 ...
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- dubbo+zookeeper+springboot简单示例
目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...
随机推荐
- 59、常规控件(2)TextInputLayout-让EditText提示更加人性化
提示语用在显示. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- CommonJS和AMD/CMD
JS中的模块规范(CommonJS,AMD,CMD) 一,CommonJS NodeJS是CommonJS规范的实现,webpack也是以CommonJS的形式来书写. 在浏览器环境下,没有模块也不是 ...
- HYSBZ 2160 拉拉队排练(回文树)
2160: 拉拉队排练 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 825 Solved: 324 [Submit][Status][Discu ...
- CodeForces 17E Palisection(回文树)
E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...
- 巨蟒python全栈开发数据库前端1:HTML基础
1.HTML介绍 什么是前端? 前端就是我们打开浏览器的页面.,很多公司都有自己的浏览器的页面,这个阶段学习的就是浏览器界面 比如京东的界面:https://www.jd.com/ 引子 例1 soc ...
- boost:property_tree::ini_parser:::read_ini 读取ini时崩溃
原因: 1 路径错误 2 配置文件中某一行缺少=,例如用// 做注释的,前面应该加";" 解决办法: 添加异常处理,实例代码如下: #include <boost/prope ...
- 让select下的option选中
这里以默认选中当前月为例: HTML: 性别 <select name="sex" id="sex"> <option value=" ...
- 总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?
首先,简单介绍下Http请求中Content-Type类型 类型格式:type/subtype(;parameter)? type 主类型,任意的字符串,如text,如果是*号代表所有: subtyp ...
- 《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes
Process Switching 1.The set of data that must be loaded into the registers before the process resume ...
- Submission Details [leetcode] 算法的改进
最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...