Java|Java SpringBoot实现AOP
目录
- 1、AOP基本总结
- 2、常用方法
- 3、增强类型
- 4、示例说明
- 5、结果展示
1、AOP基本总结
连接点(JoinPoint
):
连接点是程序运行的某个阶段点,如方法调用、异常抛出等
切入点(Pointcut
):
切入点是JoinPoint
的集合
是程序中需要注入Advice
的位置的集合,即Advice
在什么条件下才能被触发
【Java|Java SpringBoot实现AOP】增强(Advisor
):
增强是切入点Pointcut
和Advice
的综合体,即在连接点JoinPoint
上执行的行为
通过JDK/CGLIB
代理模式实现AOP
切面(Aspect
):
@Aspect通常是一个类的注解,通常与@Component
搭配使用
AOP代理(AOP Proxy
):
AOP使用动态代理模式创建对象,从而实现在连接点JoinPoint
处插入增强
其中JDK只能代理接口,CGLIB基于子类但不能代理final类
2、常用方法
文章图片
3、增强类型
@Before
:前置增强,在某个JoinPoint
执行前的增强@After
:final增强,不管抛异常还是正常退出都执行的增强@AfterReturning
:后置增强,方法正常退出时执行@AfterThrowing
:异常抛出增强,抛出异常后执行@Around
:环绕增强,包围一个连接点的增强,最强大的一个方式,且常用
4、示例说明 学了一下AOP的使用,写了个
@Around
的demo
,将几个查询操作存入数据库作为Log
并且定时清理过期数据本人的
Demo
用的是Dubbo
框架,而AOP的示例写在了Provider
中,大概结构如下:文章图片
monitor:
annotation
:注解类aop
:切面的定义及实现impl
:UserAopTask接口的实现类
UserLog
实体类@Data@ToString@NoArgsConstructor@AllArgsConstructorpublic class UserLog implements Serializable {private Integer id; private String methodName; private String methodArgs; private String classFullName; private String className; private Date invokeTime; private Double costTime; }
2)
LogTaskMapper
对应mapper
接口public interface LogTaskMapper {/*** TEST AOP INSERT INFO INTO TABLE* @param userLog*/void insertUserLog(UserLog userLog); /*** DELETE LOGS IN TABLE LAST x MINUTES* @param minutes*/void deleteUserLog(int minutes); }
3)
UserAopTask
接口public interface UserAopTask {void insertUserLog(UserLog log); }
4)
UserAopTaskImpl
实现类@Componentpublic class UserAopTaskImpl implements UserAopTask {private static final Logger logger = LoggerFactory.getLogger(UserAopTask.class); @Autowiredprivate LogTaskMapper logTaskMapper; private ExecutorService logHandler = Executors.newFixedThreadPool(1); //采用线程池复用一个线程执行private static final int MINUTES_LOG_RETAIN = 30; //数据库中数据保留时间@Overridepublic void insertUserLog(UserLog log) {logHandler.submit(new logSubmitTask(log)); }//内部类class logSubmitTask implements Runnable{private UserLog userLog; public logSubmitTask(UserLog userLog){this.userLog = userLog; }@Overridepublic void run() {logTaskMapper.insertUserLog(userLog); }}//定时清理任务@Scheduled(cron = "0 30 * * * *")public void scheduledDeleteLog(){logger.info("开始清除[{}]分钟之前的图表查询日志...", MINUTES_LOG_RETAIN); logTaskMapper.deleteUserLog(-1 * MINUTES_LOG_RETAIN); }}
5)
TestUserAop
切面类@Aspect//切面@Component//Spring容器管理public class TestUserAop {private static final Logger logger = LoggerFactory.getLogger(TestUserAop.class); @Autowiredprivate UserAopTask userAopTask; //使用环绕增强,第一参数必须是ProceedingJoinPoint@Around(value = "https://www.it610.com/article/@annotation(annotation)")//和注解类参数名保持一致public Object aroundUserInfo(ProceedingJoinPoint pjp, TestUserAnnotation annotation) throws Throwable{UserLog userLog = new UserLog(); System.out.println("=====================ANNOTATION BEGIN====================="); Date date = new Date(); Long methodStart = date.getTime(); //timestampSystem.out.println("ANNOTATION 开始耗时统计: "+ date); userLog.setInvokeTime(date); Object[] argsObj = pjp.getArgs(); Object res = pjp.proceed(argsObj); //利用反射调用目标方法Long methodCost = System.currentTimeMillis() - methodStart; double cost = methodCost/1000d; //timestamp 转换为 secondsSystem.out.println("ANNOTATION 调用方法总耗时: "+ String.format("%.3f",cost) +" s"); //保留3位小数System.out.println("ANNOTATION 调用方法: "+annotation.methodName()); //目标方法System.out.println("ANNOTATION 调用方法参数: "+ new Integer((Integer) argsObj[0])); //我的参数就1个或者无参System.out.println("ANNOTATION 调用类: "+pjp.getSignature().getDeclaringTypeName()); //全类名System.out.println("ANNOTATION 调用类名: "+pjp.getSignature().getDeclaringType().getSimpleName()); //类名System.out.println("ANNOTATION 调用结果: "+ JSON.toJSON(res)); System.out.println("=====================ANNOTATION FINISHED====================="); userLog.setCostTime(Double.parseDouble(String.format("%.3f",cost))); userLog.setClassFullName(pjp.getSignature().getDeclaringTypeName()); userLog.setClassName(pjp.getSignature().getDeclaringType().getSimpleName()); userLog.setMethodName(annotation.methodName()); userLog.setMethodArgs(Integer.toString(new Integer((Integer) argsObj[0]))); userAopTask.insertUserLog(userLog); return res; }}
6)
TestUserAnnotation
注解类我在
service
层写的AOP demo
,对目标方法使用注解,注解名为注解类名即可,如@TestUserAnnotation
@Retention(RetentionPolicy.RUNTIME)//运行时有效@Target(ElementType.METHOD)//作用于方法@Documentedpublic @interface TestUserAnnotation {String methodName() default ""; //方法名,默认为空字符串}
7)
LogTaskMapper.xml
最后贴个代码,为上面提到的定时任务,用到的是date_add()方法,其中的 "< " 意为 "<"
delete from invoke_logwhere invoke_time < date_add(current_timestamp,interval #{minutes} minute)
5、结果展示 演示一下AOP的效果,将
@TestUserAnnotation
注解在方法getUserInfo(),
即获取用户信息Demo
中利用AOP的@Around
环绕增强,实现了统计方法调用运行消耗时间,以及统计调用方法名、类名等信息:文章图片
调用方法
getUserInfo
后的统计结果:文章图片
到此这篇关于
Java SpringBoot
实现AOP 的文章就介绍到这了,更多相关SpringBoot
实现AOP 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Activiti(一)SpringBoot2集成Activiti6
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 事件代理
- 孩子不是实现父母欲望的工具——林哈夫
- SpringBoot调用公共模块的自定义注解失效的解决
- opencv|opencv C++模板匹配的简单实现
- Java|Java OpenCV图像处理之SIFT角点检测详解