对于AspectJ,AspectJ的切入点和目标的切入方法不能是在同一个类中。如果是在同一个类中,那么切面切点注入将会是无效的。
AspectJ的所有切面写在一个带有@AspectJ注解的类中
package com.liangzhifeng.demo.entity;
import org.springframework.stereotype.Component;
@Component
public class TestMain {
public String getStr(){
String result = "this is first world";
log.info("create the string: " + result);
return result;
}
}
package com.liangzhifeng.demo.entity;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ServiceMonitor {
@Before("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public void before(JoinPoint joinPoint) {
System.out.println("Completed before: " + joinPoint);
}
@Around("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public void around(JoinPoint joinPoint) {
System.out.println("Completed around: " + joinPoint);
}
@AfterReturning("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public void afterReturning(JoinPoint joinPoint) {
System.out.println("Completed afterReturning: " + joinPoint);
}
@After("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public void after(JoinPoint joinPoint) {
System.out.println("Completed after: " + joinPoint);
}
}
package com.liangzhifeng.demo.controller;
import com.liangzhifeng.demo.entity.TestMain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@Slf4j
public class TestController {
@Autowired
private TestMain testMain;
@RequestMapping(value = "/testInsert")
@ResponseBody
public String insert(){
System.out.println("get the string: " + testMain.getStr());
return "success";
}
}
执行结果
Completed aroundexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterReturning: execution(String com.liangzhifeng.demo.entity.TestMain.getStr())
get the string: null
可以看到@Before并没有被调用,并且getStr()日志没有打印,然后返回值是null,这是因为@Around影响了。为了不影响@Before的执行,我们必须在@Around方法改成如下:
@Around("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
pjp.proceed();
System.out.println("Completed around" + pjp);
}
再次执行结果如下
Completed beforeexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
2018-04-19 16:12:36.276 INFO 1620 --- [nio-8080-exec-1] com.liangzhifeng.demo.entity.TestMain : create the string: this is first world
Completed aroundexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterReturning: execution(String com.liangzhifeng.demo.entity.TestMain.getStr())
get the string: null
可以看到@Before执行了,并且方法中的日志也打印了,只是返回结果却是null。很明显肯定时@Around方法影响了(至于原因后面再说),我们去掉@Around方法。
再次执行结果如下
Completed beforeexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
2018-04-19 16:24:05.239 INFO 6412 --- [nio-8080-exec-1] com.liangzhifeng.demo.entity.TestMain : create the string: this is first world
Completed afterexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterReturning: execution(String com.liangzhifeng.demo.entity.TestMain.getStr())
get the string: this is first world
可以看到@Before方法已经被执行,并且getStr()方法数据正确返回,使用AspectJ时需要谨慎使用@Around。我们再次修改@Around方法
@Around("execution(* com.liangzhifeng.demo.entity.TestMain.getStr(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Completed around before method is executed" + pjp);
Object methodResult = pjp.proceed();
System.out.println("Completed around after method is executed" + pjp);
return methodResult;
}
执行结果如下
Completed around before method is executedexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed beforeexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
2018-04-19 16:49:00.046 INFO 9148 --- [nio-8080-exec-1] com.liangzhifeng.demo.entity.TestMain : create the string: this is first world
Completed around after method is executedexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterexecution(String com.liangzhifeng.demo.entity.TestMain.getStr())
Completed afterReturning: execution(String com.liangzhifeng.demo.entity.TestMain.getStr())
get the string: this is first world
由此我们可以大致得出AspectJ的注解的实现过程:
- 对于@Before,会在目标方法中加入@Before的逻辑体
public String getStr(){
@Before logic
}
- 对于@Around,会在目标方法执行前后加上对应的逻辑体。@Around真正执行方法的地方是ProceedingJoinPoint的processed()方法,如果不调用该方法那么,目标方法是不会被用的。
public @Around method's return type aroundLogicMethod(ProceedingJoinPoint pjp){
@Around before processed() logic
//public String getStr(){
// @Before logic
//}
pjp.processed(); //等同于 getStr()
@Around before processed() logic
return Around method's return type;
}
最终getStr()的代码如下:
public String getStr(){ //该方法只是目标方法的一个代理
return @Around aroundLogicMethod(pjp);
}
由于AspectJ代理了目标方法,返回的结果是@Around的方法的返回值,而我们刚开始是定义的@Around的返回值是void,所以我们之前调用getStr()的结果是null,如果不调用ProceedingJoinPoint.processed()方法,那么目标方法中的日志也是不会进行打印的,也就是说ProceedingJoinPoint.processed()包含了目标方法的调用,并且ProceedingJoinPoint.processed()返回的结果就是调用目标方法的返回结果。
AspectJ的原理:ApsectJ使用自己的代码编译器,直接修改Java class文件,在对应的切入点前后加入对应的的操作,并且会对目标切入点进行代理包装。
AspectJ大致逻辑模拟如下
public Object targetMethod() {
Object result = aroundMethod();
@AfterMethod();
@AfterReturning
return result;
}
public aroundMethodReturnType aroundMethod(){
pre around logic
Object result = ProceedingJoinPoint.processed();
after around logic
return aroundMethodReturnType;
}
public class ProceedingJoinPoint{
public Object processed(){
@BeforeMethod();
Object result = targetMethod();
return result;
}
}