本文介紹了Spring AOP和AspectJ采用相同的方法的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個(gè)關(guān)于使用AspectJ和Spring AOP方法攔截的問題。我創(chuàng)建了兩個(gè)批注:@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;
}
}
應(yīng)用程序類
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);
}
}
但當(dāng)我通過http://localhost:8080/test?firstParam=test&secondParam=2調(diào)用它時(shí),我看不到與方法執(zhí)行時(shí)間相關(guān)的消息,但可以看到傳遞給該方法的參數(shù)數(shù)量。如果我要?jiǎng)h除@SAOPTest-方法的執(zhí)行時(shí)間按預(yù)期工作,但它不能同時(shí)使用兩個(gè)注釋。是Spring創(chuàng)建的代理對(duì)象有問題,還是我遺漏了什么?
推薦答案
您的偵聽器未正確運(yùn)行。請(qǐng)閱讀MethodInterceptorjavadoc。攔截器應(yīng)如下所示:
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();
}
}
此外,你的相位計(jì)算時(shí)間也是錯(cuò)誤的。首先,您說finish = System.currentTimeMillis() - start,然后打印start - finish。您應(yīng)該減去finish - start或計(jì)算在變量中花費(fèi)的時(shí)間,但不能同時(shí)計(jì)算兩者,也不能同時(shí)計(jì)算start - finish。為什么不干脆System.out.println("Method execution time: " + (System.currentTimeMillis() - start));?
這篇關(guān)于Spring AOP和AspectJ采用相同的方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,






