本文介紹了Spring AOP和AspectJ采用相同的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個關于使用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創建的代理對象有問題,還是我遺漏了什么?
推薦答案
您的偵聽器未正確運行。請閱讀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));
?
這篇關于Spring AOP和AspectJ采用相同的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,