首页 > 其他分享 >AOP切面

AOP切面

时间:2024-10-27 15:47:06浏览次数:1  
标签:repeatSubmit System RepeatSubmit 切面 AOP println public out

切面修改注解内部属性值

注解

@Target(ElementType.METHOD)
// 运行时
@Retention(RetentionPolicy.RUNTIME)
// 可以出现在 生成的doc文档上
@Documented
public @interface RepeatSubmit {
    // 属性以方法的形式  可以设置默认值
    int lockTime() default 5;

    String methodName() default  "";

}

 切面

@Order(0)
@Slf4j
@Aspect
@Component
public class RepeatSubmitAspect {

    @Before("execution(* *(..))&&@annotation(com.springboot.demo.webbase.annotation.RepeatSubmit)")
    public void logExecutionTime(JoinPoint joinPoint) throws Throwable {
        System.out.println("aop start");
        long start = System.currentTimeMillis();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest  request = attributes.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        MethodSignature  signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod( );
        RepeatSubmit repeatSubmit = method.getAnnotation(RepeatSubmit.class);
        // System.out.println(repeatSubmit);
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(repeatSubmit);
        Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
        memberValues.setAccessible(true);
        Map map = (Map)memberValues.get(invocationHandler);
        System.out.println(map);
        map.put("lockTime", Integer.valueOf(String.join("",parameterMap.get("lockTime"))));
        map.put("methodName", String.join("", parameterMap.get("methodName")));
        System.out.println(repeatSubmit);

        long executionTime = System.currentTimeMillis() - start;
        log.info("Method {} execution time: {} ms", joinPoint.getSignature().toShortString(), executionTime);
        System.out.println("aop end");
    }
}

  

接口

@RequiredArgsConstructor
@RestController
@RequestMapping("/aop")
public class AopTestController {
    private final AopTestService aopTestService;

    @RepeatSubmit
    @GetMapping("/doSomeThing")
    public String doSomeThing() {
        aopTestService.doSomeThingOther();
        return "success";
    }
}

 

标签:repeatSubmit,System,RepeatSubmit,切面,AOP,println,public,out
From: https://www.cnblogs.com/deity-night/p/18508514

相关文章

  • 若依 自定义注解@Log,实现aop
    在一个controller的方法写了@Log,注解没有产生log,因为是匿名调用,LogAspect的handleLog出错了 src/main/java/com/ktg/framework/aspectj/LogAspect.java 17:46:26.628[http-nio-8085-exec-22]DEBUGc.k.m.l.m.L.deleteLbDetailByMatNo-[debug,137]-==>Preparing:del......
  • 聊一聊Spring中的AOP【XML】【标签解析】
    [!NOTE]**Spring版本:**5.3.27**AspectJ版本:**1.9.22**JDK版本:**1.81、前置说明[!TIP]概念性的东西理解起来都会比较抽象,下面的一些概念可以一扫而过,有个大致印象就行。先学会使用,再分析原理,回过头来再看这些概念就会一一对应上。1.1前置概念1.1.1基础概念AOP(......
  • 公共字段自动填充-AOP
    1.问题描述1).在新增数据时,要将createTime、updateTime设置为当前时间,createUser、updateUser设置为当前登录用户ID。2).在更新数据时,要将updateTime设置为当前时间,updateUser设置为当前登录用户ID。在所有的新增和更新的业务操作中,都需要对上述字段进行赋值操......
  • AOP - 自己写 JDK 动态代理增强 bean
    AOP的原理就是给目标对象创建代理对象,达到增强目标对象方法的目的如果目标对象实现了接口就是用JDK动态代理,如果没实现接口就是用三方的CGLIB代理如果不使用AOP想要增强一个bean可以这样做:@ComponentpublicclassTestimplementsBeanPostProcessor,ApplicationCon......
  • AOP - 切点表达式
    某个特殊的方法:com.example.service.UserService类中所有以find开头的公共方法execution(public*com.example.service.UserService.find*(..))类中的所有方法:com.example.service包下所有类的所有方法execution(*com.example.service.*.*(..))特定参数类型的方法......
  • AOP - Advisor 示例
    定义通知publicclassLoggingAdviceimplementsMethodInterceptor{@OverridepublicObjectinvoke(MethodInvocationinvocation)throwsThrowable{System.out.println("Method"+invocation.getMethod().getName()+"isbeingcalle......
  • AOP - AspectJ 示例
    //自定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public@interfaceLogExecution{}@Aspect//切面类@Order(1000)//数字越小,优先级越高@Component//也要注册到容器publicclassLoggingAspect{//定义切点@Pointcut("ex......
  • SpringAop学习笔记
    SpringAop学习笔记文章目录SpringAop学习笔记1.面向切面编程(AOP)1.1代理模式1.2静态代理1.3动态代理2.AOP概念及相关术语2.1概述2.2相关术语①横切关注点②通知(增强)③切面④目标⑤代理⑥连接点⑦切入点2.3作用3.基于注解的AOP3.1技术说明3.2准备工作3.3创......
  • AOP
    SpringAOPOOP:Object-OrientedProgramming,面向对象编程;AOP:Aspect-OrientedProgramming,面向切面编程Advisor:spring自己的AOP组件;AspectJ:三方实现的AOP组件底层对bean创建代理对象,达到增强目的。如果目标bean实现了接口,就用JDK动态代理;如果没有,就用CGLIB声明式......
  • SpringBoot Aop面向切面编程-快速入门-实战案例
    AOP部分笔记来自黑马程序员。一、AOP概述什么是AOP?AOP英文全称:AspectOrientedProgramming(面向切面编程、面向方面编程),其实说白了,面向切面编程就是面向特定方法编程。那什么又是面向方法编程呢,为什么又需要面向方法编程呢?来我们举个例子做一个说明:比如,我们这里有一个......