JAVA基础整理(9)AOP

@RestController
@RequestMapping("/hello")
@RefreshScope
public class Hello {
    public String name2 = "CC";

    @AnnTest2(name = "test")
    @GetMapping("/ann3")
    public String ann3() {
        //切面类
        String name = "BB";

        System.out.println(name);
        System.out.println(this.name2);
        return "ok";
    }
}

package com.example.web1_2.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

@Aspect  //1. 声明一个切面类
@Component
public class AnnTestAspect {
    // 2. PointCut表示这是一个切点,@annotation表示这个切点切到一个注解上,后面带该注解的全类名
    // 切面最主要的就是切点,所有的故事都围绕切点发生
    // logPointCut()代表切点名称
    @Pointcut("@annotation(com.example.web1_2.annotation.AnnTest2)")
    public void testPointCut(){};

    // 3. 环绕通知
    @Around("testPointCut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        // 获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //ann3

        // 获取入参
        Object[] param = joinPoint.getArgs();

        StringBuilder sb = new StringBuilder();

        for (Object o : param) {
            sb.append(o + "; ");
        }
        //System.out.println("进入[" + methodName + "]方法,参数为:" + sb.toString());


        //aop代理对象 获取代理对象自己
        Object aThis = joinPoint.getThis();
        //System.out.println(aThis.toString());
        //com.example.web1_2.controller.Hello@13e1bbc6
        //被代理对象
        Object target = joinPoint.getTarget();
        //System.out.println(target.toString());
        //com.example.web1_2.controller.Hello@13e1bbc6

        //获取连接点的方法签名对象
        Signature signature = joinPoint.getSignature();
        //System.out.println(signature.toLongString());
        //public java.lang.String com.example.web1_2.controller.Hello.ann3()

        //System.out.println(signature.toShortString());
        //Hello.ann3()

        //System.out.println(signature.toString());
        //String com.example.web1_2.controller.Hello.ann3()

        //获取方法名
        //System.out.println(signature.getName());
        //ann3

        //获取声明类型名
        //System.out.println(signature.getDeclaringTypeName());
        //com.example.web1_2.controller.Hello

        //获取声明类型  方法所在类的class对象
        //System.out.println(signature.getDeclaringType().toString());
        //class com.example.web1_2.controller.Hello

        //和getDeclaringTypeName()一样
        //System.out.println(signature.getDeclaringType().getName());
        //com.example.web1_2.controller.Hello

        //连接点类型
        String kind = joinPoint.getKind();
        //System.out.println(kind);
        //method-execution

        //返回连接点静态部分
        JoinPoint.StaticPart staticPart = joinPoint.getStaticPart();
        //System.out.println(staticPart.toLongString());
        //execution(public java.lang.String com.example.web1_2.controller.Hello.ann3())

        //attributes可以获取request信息 session信息等
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //System.out.println(request.getRequestURL().toString());
        //http://192.168.1.18:40012/hello/ann3

        //System.out.println(request.getRemoteAddr());
        //192.168.1.234
        //System.out.println(request.getMethod());
        //GET

        //获取详细方法签名对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();//获取到方法对象
        AnnTest2 annTest = method.getAnnotation(AnnTest2.class); //获取方法上的注解
        //System.out.println(annTest);
        //System.out.println(annTest.name());
        //@com.example.web1_2.annotation.AnnTest2(name=test)


        Class cla = joinPoint.getSignature().getDeclaringType();
        Field fields = cla.getDeclaredField("name2");
        //拦截的实体类
        Object obj4 = joinPoint.getTarget();
        fields.setAccessible(true);
        fields.set(obj4, annTest.name());
        //System.out.println(fields.get(obj4));


        // 继续执行方法
        try {

            //运行程序
            return joinPoint.proceed();

        } catch (Throwable throwable) {

            throwable.printStackTrace();

        }
        System.out.println("方法执行结束");
        return null;
    }
}

你可能感兴趣的:(java,开发语言)