程序笔记   发布时间:2022-07-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SpringBoot AOP Demo大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

AOP基本总结

  • 连接点(JoinPoint):
    • 连接点是程序运行的某个阶段点,如方法调用、异常抛出等
  • 切入点(Pointcut):
    • 切入点是JoinPoint的集合
    • 是程序中需要注入Advice的位置的集合,即Advice在什么条件下才能被触发
  • 增强(Advisor):
    • 增强是切入点Pointcut和Advice的综合体,即在连接点JoinPoint上执行的行为
    • 通过JDK/CGLIB代理模式实现AOP
  • 切面(Aspect):
    • @Aspect通常是一个类的注解,通常与@Component搭配使用
  • AOP代理(AOP Proxy):
    • AOP使用动态代理模式创建对象,从而实现在连接点JoinPoint处插入增强
    • 其中JDK只能代理接口,CGLIB基于子类但不能代理final类

 

常用方法

方法   描述
Object[] getArgs 返回目标方法的参数数组
Signature getSignature 返回目标方法所在类信息
Object getTarget 返回被织入增强处理的目标对象
Object getThis

返回AOP代理对象

Object proceed(Object[] args)

利用反射执行目标方法并返回结果

 

 

 

 

 

 

 

 

增强类型

  • @Before:前置增强,在某个JoinPoint执行前的增强
  • @After:final增强,不管抛异常还是正常退出都执行的增强
  • @AfterReturning:后置增强,方法正常退出时执行
  • @AfterThrowing:异常抛出增强,抛出异常后执行
  • @Around:环绕增强,包围一个连接点的增强,最强大的一个方式,且常用

 

示例说明

  学了一下AOP的使用,写了个@Around的demo,将几个查询操作存入数据库作为Log并且定时清理过期数据

  1)UserLog实体类

 1 @Data
 2 @ToString
 3 @NoArgsConstructor
 4 @AllArgsConstructor
 5 public class UserLog implements serializable {
 6     private Integer id;
 7     private String methodName;
 8     private String methodArgs;
 9     private String classFullName;
10     private String className;
11     private Date invokeTime;
12     private Double costTime;
13 }

  2)LogTaskMapper对应mapper接口

 1 public interface LogTaskMapper {
 2     /**
 3      * TEST AOP INSERT INFO INTO TABLE
 4      * @param userLog
 5      */
 6     void insertUserLog(UserLog userLog);
 7 
 8     /**
 9      * deletE LOGS IN TABLE LAST 2 DAYS
10      * @param minutes
11      */
12     void deleteUserLog(int minutes);
13 }

  3)UserAopTask接口

1 public interface UserAopTask {
2     void insertUserLog(UserLog log);
3 }

  4)UserAopTaskImpl实现类

 1 @Component
 2 public class UserAopTaskImpl implements UserAopTask {
 3 
 4     private static final Logger logger = LoggerFactory.getLogger(UserAopTask.class);
 5 
 6     @Autowired
 7     private LogTaskMapper logTaskMapper;
 8 
 9     private Executorservice logHandler = Executors.newFixedThreadPool(1);//采用线程池复用一个线程执行
10 
11     private static final int minutES_LOG_RETAIN = 30;//数据库中数据保留时间
12 
13 
14     @Override
15     public void insertUserLog(UserLog log) {
16         logHandler.submit(new logSubmitTask(log));
17     }
18 
19     //内部类
20     class logSubmitTask implements Runnable{
21 
22         private UserLog userLog;
23 
24         public logSubmitTask(UserLog userLog){
25             this.userLog = userLog;
26         }
27 
28         @Override
29         public void run() {
30             logTaskMapper.insertUserLog(userLog);
31         }
32     }
33     
34     //定时清理任务
35     @scheduled(cron = "0 30 * * * *")
36     public void scheduleddeleteLog(){
37         logger.info("开始清除[{}]分钟之前的图表查询日志...", minutES_LOG_RETAIN);
38         logTaskMapper.deleteUserLog(-1 * minutES_LOG_RETAIN);
39     }
40 
41 }

  5)TestuserAop切面类

 1 @Aspect//切面
 2 @Component//Spring容器管理
 3 public class TestuserAop {
 4     private static final Logger logger = LoggerFactory.getLogger(TestuserAop.class);
 5 
 6     @Autowired
 7     private UserAopTask userAopTask;
 8    //使用环绕增强,第一参数必须是ProceedingJoinPoint
 9     @Around(value = "@Annotation(Annotation)")//和注解类参数名保持一致
10     public Object aroundUserInfo(ProceedingJoinPoint pjp, TestuserAnnotation Annotation) throws Throwable{
11         UserLog userLog = new UserLog();
12 
13         System.out.println("=====================AnnOTATION BEGIN=====================");
14 
15         Date date = new Date();
16         Long methodStart = date.getTime();//timestamp
17         System.out.println("AnnOTATION 开始耗时统计: "+ datE);
18 
19         userLog.seTinvokeTime(datE);
20 
21         Object[] argsObj = pjp.getArgs();
22         Object res = pjp.proceed(argsObj);//利用反射调用目标方法
23 
24         Long methodCost = System.currentTimeMillis() - methodStart;
25         double cost = methodCost/1000d;//timestamp 转换为 seconds
26 
27         System.out.println("AnnOTATION 调用方法总耗时: "+ String.format("%.3f",cost) +" s");//保留3位小数
28         System.out.println("AnnOTATION 调用方法: "+Annotation.methodName());//目标方法
29         System.out.println("AnnOTATION 调用方法参数: "+ new Integer((Integer) argsObj[0]));//我的参数就1个或者无参
30         System.out.println("AnnOTATION 调用类: "+pjp.getSignature().getDeclaringTypename());//全类名
31         System.out.println("AnnOTATION 调用类名: "+pjp.getSignature().getDeclaringType().getSimplename());//类名
32         System.out.println("AnnOTATION 调用结果: "+ JSON.toJSON(res));
33         System.out.println("=====================AnnOTATION FINISHED=====================");
34 
35         userLog.setCostTime(Double.parseDouble(String.format("%.3f",cost)));
36         userLog.setClassFullName(pjp.getSignature().getDeclaringTypename());
37         userLog.setClassName(pjp.getSignature().getDeclaringType().getSimplename());
38         userLog.setMethodName(Annotation.methodName());
39         userLog.setMethodArgs(Integer.toString(new Integer((Integer) argsObj[0])));
40 
41         userAopTask.insertUserLog(userLog);
42 
43         return res;
44 
45     }
46 }

  6)TestuserAnnotation注解类

我在service层写的AOP demo,对目标方法使用注解,注解名为注解类名即可,如@TestuserAnnotation

1 @Retention(RetentionPolicy.RUNTIME)//运行时有效
2 @Target(ElementType.METHOD)//作用于方法
3 @Documented
4 public @interface TestuserAnnotation {
5     String methodName() default "";//方法名,默认为空字符串
6 }

大佬总结

以上是大佬教程为你收集整理的SpringBoot AOP Demo全部内容,希望文章能够帮你解决SpringBoot AOP Demo所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。