SpringBoot之AOP切面的使用
相关文章:
Spring AOP切面使用详细解析
SpringBoot之Listener注册到Spring容器中的多种方法
SpringBoot之Interceptor拦截器注入使用
SpringBoot之Filter过滤器的实现及排序问题
SpringBoot 之多个过滤器(Filter) ,监听器(Listener),切面(AOP),拦截器(Interceptor)的指定排序问题总结篇
1、开启AOP
在Application启动器上添加@EnableAspectJAutoProxy注解;
或者在添有@Configuration的配置类上添加@EnableAspectJAutoProxy;
参数解析
proxyTargetClass 属性,默认为false,表示使用jdk动态代理织入增强,当配为true时,表示使用CGLib动态代理技术织入增强。不过即使设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
相关源码:
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {@Overridepublic AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {Class<?> targetClass = config.getTargetClass();if (targetClass == null) {throw new AopConfigException("TargetSource cannot determine target class: " +"Either an interface or a target is required for proxy creation.");}if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {return new JdkDynamicAopProxy(config);}return new ObjenesisCglibAopProxy(config);}else {return new JdkDynamicAopProxy(config);}}/*** Determine whether the supplied {@link AdvisedSupport} has only the* {@link org.springframework.aop.SpringProxy} interface specified* (or no proxy interfaces specified at all).*/private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {Class<?>[] ifcs = config.getProxiedInterfaces();return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));}}
exposeProxy 属性 默认为false 控制代理的暴露方式,解决内部调用不能使用代理的场景。
2、使用注解@Aspect
package com.dbgs.core.aspect;import com.dbgs.core.common.RequestBean;
import com.dbgs.core.dto.Base;
import com.dbgs.core.dto.DownLoadDto;
import com.dbgs.core.dto.PropertyDto;
import com.dbgs.core.dto.QybViewDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;package com.jarvisy.demo.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;//声明这是一个组件
@Component
//声明这是一个切面 Bean
@Aspect
@Slf4j
public class AnnotationAspect {//配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点@Pointcut("execution(* com.jarvisy.demo.pattern.*(..))")public void aspect() {}/** 配置前置通知,使用在方法 aspect()上注册的切入点* 同时接受 JoinPoint 切入点对象,可以没有该参数*/@Before("aspect()")public void before(JoinPoint joinPoint) {log.info("before 通知 " + joinPoint);}//配置后置通知,使用在方法 aspect()上注册的切入点@After("aspect()")public void after(JoinPoint joinPoint) {log.info("after 通知 " + joinPoint);}//配置环绕通知,使用在方法 aspect()上注册的切入点@Around("aspect()")public void around(JoinPoint joinPoint) {long start = System.currentTimeMillis();try {((ProceedingJoinPoint) joinPoint).proceed();long end = System.currentTimeMillis();log.info("around 通知 " + joinPoint + "\tUse time : " + (end - start) + " ms!");} catch (Throwable e) {long end = System.currentTimeMillis();log.info("around 通知 " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());}}//配置后置返回通知,使用在方法 aspect()上注册的切入点@AfterReturning("aspect()")public void afterReturn(JoinPoint joinPoint) {log.info("afterReturn 通知 " + joinPoint);}//配置抛出异常后通知,使用在方法 aspect()上注册的切入点@AfterThrowing(pointcut = "aspect()", throwing = "ex")public void afterThrow(JoinPoint joinPoint, Exception ex) {log.info("afterThrow 通知 " + joinPoint + "\t" + ex.getMessage());}
}
3、多个AOP指定执行顺序
第一种:实现Ordered接口,数值越小越先执行
第二种,@order注解,数值越小越先执行
SpringBoot之AOP切面的使用相关推荐
- aop springboot 传入参数_java相关:springboot配置aop切面日志打印过程解析
java相关:springboot配置aop切面日志打印过程解析 发布于 2020-3-31| 复制链接 摘记: 这篇文章主要介绍了springboot配置aop切面日志打印过程解析,文中通过示例代码 ...
- java 切面 不执行,解决springboot的aop切面不起作用问题(失效的排查)
检查下springboot的启动类是否开启扫描 @springbootapplication @componentscan(basepackages = {"com.zhangpu.spri ...
- springboot之aop切面获取请求
springboot之aop切面获取请求 项目场景: 在学习springboot的博客开发中,通过aop切面,对博客中的操作进行记录 问题描述: 问题: 在切面方法中,无法获取请求的参数和类名,方法, ...
- springboot实现AOP切面编程
概述 AOP(Aspect Oriented Programming) 即面向切面编程.面向切面是面向对象中的一种方式而已.在代码执行过程中,动态嵌入其他代码,叫做面向切面编程(将交叉业务逻辑封装成成 ...
- springboot配置aop切面详解
1 引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>sp ...
- springboot中面向切面编程(AOP)
文章目录 1.什么是AOP 2.springboot使用aop 1.什么是AOP AOP就是通过动态为项目中某些类在运行过程中动态创建代理对象,在对象中完成附加功能,保证在处理业务时更加专注于自己的业 ...
- SpringBoot—集成AOP详解(面向切面编程Aspect)
关注微信公众号:CodingTechWork,一起学习进步. AOP介绍 AOP概述 AOP是Aspect-Oriented Programming,即为面向(切面)方面编程.在维基百科中的解释: ...
- SpringBoot之AOP面向切面编程
什么是AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开 ...
- Springboot AOP切面
文章目录 SpringBoot Aop 切面(Aop) 一.什么是切面 二.切面的用途 三.AOP切面常用注解 四.详细内容 1.切面(Aspect) 2.连接点(Joinpoint) 3.通知(Ad ...
最新文章
- 在启动activity之前,调用application的oncreate
- HBase之HFile解析
- 学习java应该如何理解反射?
- 互斥事件的概念和公式_IGCSE数学5月大考冲刺A*?必备公式与技巧
- 美团科技 Java工程师_美团网java工程师面试都会问哪些问题?
- navicat连接mysql报10061错
- 【2018.5.12】模拟赛之三-ssl2415 连通块【并查集】
- iMac 是什么?苹果2021新款 iMac 购买建议
- 面试精讲之面试考点及大厂真题 - 分布式专栏 15 如何解决消息重复,保证消息顺序问题
- php页面价格排序代码,php 数组动态添加实现代码(最土团购系统的价格排序)
- 数字媒体播放器行业调研报告 - 市场现状分析与发展前景预测
- 《数据科学家修炼之道》笔记
- matlab红色爱心,心形图的matlab实现
- Atitit jsr规范化分类 attilax总结
- 【开源】Java身份证号码识别系统
- 简述人工智能的发展历程图_人工智能的历程、现状及未来发展趋势
- python学习教程34-Excel生成折线图
- 3D文件压缩库——Draco简析
- establish connection
- idea中的常用快捷键(新手必看)
热门文章
- 路由跳转的时候地址栏的地址变了 但是页面不变_斐讯路由器如何设置上网 斐讯路由器设置上网方法【图文】...
- Android利用soap WSDL与Webservice通信
- Java学习系列(十七)Java面向对象之开发聊天工具
- android prgoressBar setProgressDrawable 在4.0系统式正常,在2.3系统上不能正常使用的问题...
- ssh整合步骤之二(架构设计)
- TextView滚动功能的实现
- Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
- java soap api操作和发送soap消息
- 在回调中获取Url参数
- IO中同步、异步与阻塞、非阻塞的区别