当前位置: 代码迷 >> 综合 >> springBoot + Aspect 通过注解实现代理
  详细解决方案

springBoot + Aspect 通过注解实现代理

热度:66   发布时间:2024-02-04 14:29:36.0

1.pom

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

2.注解

package com.gray.demo.core.proxy2;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*** 缓存更新注解*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheUpdate {String tableName() default "";String key() default "";String value() default "";CacheTypeEnum type() default CacheTypeEnum.REDIS;
}

3.Aspect 代理类

package com.gray.demo.core.proxy2;import com.google.gson.Gson;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import java.lang.annotation.Annotation;
import java.lang.reflect.Method;@Aspect
@Component
public class SysLogAspect {private static final String annotationPath = "com.gray.demo.core.proxy2.CacheUpdate";@Pointcut("@annotation(com.gray.demo.core.proxy2.CacheUpdate)")public void logPointCut() {}@Around("logPointCut()")public Object around(ProceedingJoinPoint point) throws Throwable {long beginTime = System.currentTimeMillis();System.out.println("环绕start");//执行方法Object result = point.proceed();//执行时长(毫秒)long time = System.currentTimeMillis() - beginTime;//保存日志System.out.println("环绕end");return result;}@Before("logPointCut()")public void before(JoinPoint joinPoint) throws Throwable {Object args[] = joinPoint.getArgs();MethodSignature signature = (MethodSignature)joinPoint.getSignature();Method method = signature.getMethod();
//        System.out.println("传入参数" + method.getDeclaringClass().getName());
//        System.out.println("传入参数" + method.getName());
//        System.out.println("传入参数" + new Gson().toJson(args));}@AfterReturning(value = "logPointCut()",returning = "rvt")public void after(JoinPoint joinPoint,Object rvt) throws Throwable {Class<? extends Annotation> methodAnnotation = Class.forName(annotationPath).asSubclass(Annotation.class);Object args[] = joinPoint.getArgs();MethodSignature signature = (MethodSignature)joinPoint.getSignature();Method method = signature.getMethod();System.out.println("传入参数" + method.getDeclaringClass().getName());System.out.println("传入参数" + method.getName());//        传入参数名
//        method.getParameters();System.out.println("传入参数" + new Gson().toJson(args));System.out.println("返回参数" + new Gson().toJson(rvt));//注解信息Annotation methodAn = method.getAnnotation(methodAnnotation);if(methodAn == null){return;}Class<?> c = methodAn.getClass();//注解方法参数String tableName = String.valueOf(c.getMethod("tableName").invoke(methodAn));String key = String.valueOf(c.getMethod("key").invoke(methodAn));String value = String.valueOf(c.getMethod("value").invoke(methodAn));CacheTypeEnum type = (CacheTypeEnum)c.getMethod("type").invoke(methodAn);System.out.println("注解信息:" + tableName);System.out.println("注解信息:" + key);System.out.println("注解信息:" + value);System.out.println("注解信息:" + type);}
}

3.工具枚举

package com.gray.demo.core.proxy2;import lombok.Getter;/*** Created on 2020/7/30.** @author Gray* @since 1.0*/
@Getter
public enum CacheTypeEnum {REDIS(1,"redis","redis缓存"),CHANNEL(2,"channel","channel缓存"),NATIVE(3,"native","本地缓存"),;CacheTypeEnum(int id, String name, String descript){this.id = id;this.name = name;this.descript = descript;}private int id;private String name;private String descript;
}

4.测试类

@RestController
public class TestController {/*** 修改登录用户密码*/@CacheUpdate(type = CacheTypeEnum.REDIS,tableName = "playerInfo", key = "playerInfo",value = "playerId")@RequestMapping("/password")public Object password(String password, String playerId){System.out.println("old:" + password + ",playerId:" + playerId);return "ok";}
}