探祕Spring AOP
一,5種Advice註解的概述
![在這裏輸入圖片標題 輸入圖片說明](http://static.javashuo.com/static/loading.gif)
二,代碼演示
/**
*
* @Before("matchAnno()")
* @After("matchAnno())") //至關於finally
* @AfterReturning("matchException()")
* @AfterThrowing("matchException()")
* @Around("matchException()")
* @Before(value = "matchLongArg() && args(productId)")
* public void beforeWithArgs(Long productId)
* @AfterReturning(value = "matchReturn()",returning = "returnValue")
* public void getReulst(Object returnValue)
*/
@Aspect
@Component
public class AdviceAspectConfig {
/******pointcut********/
@Pointcut("@annotation(cn.evchar.aop.anno.AdminOnly) && within(cn.evchar.aop..*)")
public void matchAnno(){}
@Pointcut("execution(* *..find*(Long)) && within(cn.evchar.aop..*) ")
public void matchLongArg(){}
@Pointcut("execution(public * cn.evchar.aop.service..*Service.*(..) " +
"throws java.lang.IllegalAccessException) && within(cn.evchar.aop..*)")
public void matchException(){}
@Pointcut("execution(String cn.evchar.aop..*.*(..)) && within(cn.evchar.aop..*)")
public void matchReturn(){}
/******advice********/
@After("matchAnno()")
public void after(){
System.out.println("#### after");
}
@AfterThrowing("matchException()")
public void after(){
System.out.println("#### after throwing");
}
@AfterReturning(value = "matchReturn()",returning = "result")
public void after(Object result){
System.out.println(" #### after returning result爲:" + result);
}
@Around("matchAnno()")
public Object after(ProceedingJoinPoint joinPoint){
System.out.println("### before");
Object res = null;
try {
res = joinPoint.proceed(joinPoint.getArgs());
System.out.println("### after returning");
} catch (Throwable throwable) {
System.out.println("######ex");
throwable.printStackTrace();
} finally {
System.out.println("#### finally");
}
return res;
}
@Before("matchLongArg() && args(productId)")
public void after(Long productId){
System.out.println("### before get args "+productId);
}