Java面试题 P43:框架篇:Spring-什么是AOP,你们项目中有没有使用AOP

 

 

 

 

 引入切面类依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

 

Log.class

 

 1 import java.lang.annotation.*;
 2 @Target({ElementType.PARAMETER,ElementType.METHOD})
 3 @Retention(RetentionPolicy.RUNTIME)
 4 @Documented
 5 public @interface Log {
 6     /**
 7      * 模块名称
 8      * @return
 9      */
10     public String name() default "";
11 }

 

 

 1 package com.atguigu.aop;
 2 
 3 import com.atguigu.annotation.Log;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.Signature;
 6 import org.aspectj.lang.annotation.Around;
 7 import org.aspectj.lang.annotation.Aspect;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 import org.aspectj.lang.reflect.MethodSignature;
10 import org.springframework.stereotype.Component;
11 import org.springframework.web.bind.annotation.RequestAttribute;
12 import org.springframework.web.context.request.RequestAttributes;
13 import org.springframework.web.context.request.RequestContextHolder;
14 import org.springframework.web.context.request.ServletRequestAttributes;
15 
16 import javax.servlet.http.HttpServletRequest;
17 import java.lang.reflect.Method;
18 import java.util.Date;
19 
20 @Component
21 @Aspect //切面类
22 public class SysAspect {
23     /**
24      * 切入点
25      */
26     @Pointcut("@annotation(com.atguigu.annotation.Log)")
27     private void pointcut()
28     {
29 
30     }
31 
32     /**
33      * 连接点 环绕通知
34      * @param joinPoint
35      * @return
36      * @throws Throwable
37      */
38     @Around("pointcut()")
39     public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
40         //获取用户名
41 
42         //获取被增强类和方法的信息
43         Signature signature=joinPoint.getSignature();
44         MethodSignature methodSignature=(MethodSignature) signature;
45         //获取被增强的方法对象
46         Method method=methodSignature.getMethod();
47         //从方法中解析注解
48         if(method!=null)
49         {
50             Log logAnnotation=method.getAnnotation(Log.class);
51             System.out.println(logAnnotation.name());
52         }
53         //方法名
54         String name=method.getName();
55         System.out.println(name);
56         //通过工具类获取Request对象
57         RequestAttributes reqa= RequestContextHolder.getRequestAttributes();
58         ServletRequestAttributes sra=(ServletRequestAttributes) reqa;
59         HttpServletRequest request=sra.getRequest();
60         //访问的url
61         String url=request.getRequestURI().toString();
62         System.out.println(url);
63         //请求方式
64         String methodName=request.getMethod();
65         System.out.println(methodName);
66 
67         //登录IP
68         String ipAddr="";
69         System.out.println(ipAddr);
70 
71         System.out.println(new Date());
72 
73         return joinPoint.proceed();
74     }
75

结果:

 

 

 

 

posted on 2023-08-02 23:55  wuzx-blog  阅读(24)  评论(0编辑  收藏  举报