Skip to content

Instantly share code, notes, and snippets.

@RitterHou
Created March 10, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitterHou/68ad129f70714a9ce1f5503dcdaa3194 to your computer and use it in GitHub Desktop.
Save RitterHou/68ad129f70714a9ce1f5503dcdaa3194 to your computer and use it in GitHub Desktop.
spring-boot 中使用注解方式来进行 AOP 操作
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @author of2603 hourui
* @date 2017-03-09
*/
@Aspect
@Component
public class AspectTest {
@AfterReturning("execution(* com.example.test..*.*(..))")
public void after(JoinPoint joinpoint) {
System.out.println("log: " + joinpoint);
}
@AfterReturning(returning = "rt", pointcut = "execution(* com.example.test.*.*(..))")
public void after(String rt) {
System.out.println("返回的数据是:" + rt);
}
@Before("execution(* com.example.test.*.*(..))")
public void before() {
System.out.println("之前就要执行啊...");
}
@Around("pointcut1111() && @annotation(java.lang.Deprecated)")
public Object processTx(ProceedingJoinPoint jp)throws java.lang.Throwable {
System.out.println("执行目标方法之前,模拟开始事务 ...");
// 执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(new String[]{"被改变的参数"});
System.out.println("执行目标方法之后,模拟结束事务 ...");
return rvt + " 新增的内容";
}
@Pointcut("execution(* com.example.test.*.*(..))")
public void pointcut1111() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment