Created
March 10, 2017 08:25
-
-
Save RitterHou/68ad129f70714a9ce1f5503dcdaa3194 to your computer and use it in GitHub Desktop.
spring-boot 中使用注解方式来进行 AOP 操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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