Spring AOP和AspectJ采用相同的方法

2022-05-09 00:00:00 spring java spring-boot aspectj spring-aop

我有一个关于使用AspectJ和Spring AOP方法拦截的问题。我创建了两个批注:@AJTest@SAOPTest

package com.test.company;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AJTestAspect {

    @Pointcut("@annotation(AJTest)")
    public void aJTest() {
    }

    @Around("aJTest()")
    public Object profile(ProceedingJoinPoint joinPoint) throws Throwable {
        final long start = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis() - start;
            System.out.println("Method execution time: " + (start - finish));
        }
    }
}

已注册

package com.test.company;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AJConfiguration {

    @Bean
    public AJTestAspect ajTestAspect() {
        return new AJTestAspect();
    }
}

和其他

package com.test.company;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SAOPInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Number of parameters " + methodInvocation.getArguments().length);
        return methodInvocation.getMethod().invoke(methodInvocation.getThis(), methodInvocation.getArguments());
    }
}

并注册

package com.test.company;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
public class SpringSAOPTestConfiguration {

    @Bean
    public Advisor springAopTestAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("@annotation(com.test.company.SAOPTest)");
        return new DefaultPointcutAdvisor(pointcut, new SAOPInterceptor());
    }
}

并将其添加到控制器中的我的方法

package com.test.company;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class TestController {
    
        @SAOPTest
        @AJTest
        @GetMapping("/test")
        public String doSomething(@RequestParam("firstParam") String firstParam, @RequestParam("secondParam") Integer secondParam) throws InterruptedException {
            Thread.sleep(2_500);
            return firstParam + " " + secondParam;
        }
    }

应用程序类

package com.test.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@SpringBootApplication
public class AopTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(AopTestApplication.class, args);
    }
}

但当我通过http://localhost:8080/test?firstParam=test&secondParam=2调用它时,我看不到与方法执行时间相关的消息,但可以看到传递给该方法的参数数量。如果我要删除@SAOPTest-方法的执行时间按预期工作,但它不能同时使用两个注释。是Spring创建的代理对象有问题,还是我遗漏了什么?


解决方案

您的侦听器未正确运行。请阅读MethodInterceptorjavadoc。拦截器应如下所示:

public class SAOPInterceptor implements MethodInterceptor {
  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    System.out.println("Number of parameters " + methodInvocation.getArguments().length);
    return methodInvocation.proceed();
  }
}

此外,你的相位计算时间也是错误的。首先,您说finish = System.currentTimeMillis() - start,然后打印start - finish。您应该减去finish - start或计算在变量中花费的时间,但不能同时计算两者,也不能同时计算start - finish。为什么不干脆System.out.println("Method execution time: " + (System.currentTimeMillis() - start));

相关文章