Spring AOP 和 AspectJ 在同一方法上

Spring AOP and AspectJ on same method

我有一个关于使用aspectJ 和spring aop 方法拦截的问题。我创建了 2 个注释:@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;
        }
    }

申请class

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 创建的代理对象的问题,还是我遗漏了什么?

您的拦截器未正确进行。请阅读 MethodInterceptor javadoc。拦截器应如下所示:

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));