aop幾個常用的切入點指示符匹配規則

1.execution:用於匹配方法執行的連接點

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
其中各項的語義如下
modifiers-pattern:方法的可見性,如public,protected;
ret-type-pattern:方法的返回值類型,如int,void等;
declaring-type-pattern:方法所在類的全路徑名,如com.spring.Aspect;
name-pattern:方法名類型,如buisinessService();
param-pattern:方法的參數類型,如java.lang.String;throws-pattern:
方法拋出的異常類型,如java.lang.Exception;
example:execution(public * com.spring.service.BusinessObject.businessService(java.lang.String,..))
package com.htt.app.loan.service.aop;

import com.htt.app.loan.api.annotation.CheckAop;
import org.aspectj.lang.annotation.*;

/**
 * Created by sunnyLu on 2017/6/13.
 */
@Aspect
public class AopCache {

//  @Pointcut("execution (* com.htt.app.loan.service.impl.*.*(..))")
    @Pointcut("execution (* com.htt.app.loan.service.impl.LoanServiceImpl.getLoan(long,String)) && args(p1,p2)")
    public void pointcut(long p1,String p2){}

    @Before(value = "pointcut(p1,p2)")
    public void validate(long p1,String p2) throws Throwable {
        System.out.println("進入前置通知");
        System.out.println("參數:"+p1+";"+p2);
    }

}

匹配LoanService的getLoan(long arg,String arg)方法,且可以得到傳入參數

 

2.withinany join point (method execution only in Spring AOP) within the service package or a sub-package

 

package com.htt.app.loan.service.aop;

import com.htt.app.loan.api.annotation.CheckAop;
import org.aspectj.lang.annotation.*;

/**
 * Created by sunnyLu on 2017/6/13.
 */
@Aspect
public class AopCache {

    @Pointcut(value = "within(com.htt.app.loan.service.impl..*)")
    public void pointcut(){}

    @Before(value = "pointcut()")
    public void validateAnnotation(JoinPoint joinPoint) throws Throwable {
        System.out.println(joinPoint.getArgs());
    }

}

匹配包及其子包下所有的方法,其最小粒度爲類

 

 

3.this:any join point (method execution only in Spring AOP) where the proxy implements the LoanService interface

匹配目標對象的代理對象類型是指定類型(jkd動態代理基於接口,其代理對象繼承了proxy,實現了目標接口;cglib動態代理基於類,其代理對象是目標對象的子類,這裏須要注意。)請與target區分,後者是匹配目標對象。

package com.htt.app.loan.service.aop;

import com.htt.app.loan.api.annotation.CheckAop;
import org.aspectj.lang.annotation.*;

/**
 * Created by sunnyLu on 2017/6/13.
 */
@Aspect
public class AopCache {

    @Pointcut(value = "this(com.htt.app.loan.api.service.LoanService) && args(p1,p2)")
    public void pointcut(long p1,String p2){}

    @Before(value = "pointcut(p1,p2)")
    public void validateAnnotation(long p1,String p2) throws Throwable {
        System.out.println("進入前置通知");
        System.out.println("參數:"+p1+";"+p2);
    }

}

匹配所有LoanService的接口且爲以上兩個參數的

 

 

4.@annotation:用於匹配當前執行方法持有指定註解的方法

自定義的註解

 

package com.htt.app.loan.api.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by sunnyLu on 2017/7/14.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CheckAop {
    String value() default "no description";
}

在接口上使用該註解

 

 

@Service("LoanServiceImpl")
public class LoanServiceImpl extends ServiceBase implements LoanService {

    @Override
    @CheckAop
    public BigDecimal testAop(TestParam testParam) {
        return null;
    }

}

切入點配置

 

 

package com.htt.app.loan.service.aop;

import com.htt.app.loan.api.annotation.CheckAop;
import org.aspectj.lang.annotation.*;

/**
 * Created by sunnyLu on 2017/6/13.
 */
@Aspect
public class AopCache {

    @Pointcut(value="@annotation(ano)",argNames = "ano")
    public void pointcut(CheckAop ano){}

    @Before(value = "pointcut(ano)")
    public void validateAnnotation(CheckAop ano) throws Throwable {
        System.out.println("註解式通知"+ano.value());
    }

}

 

5.@within:any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation

自定義的類級別註解

 

package com.htt.app.loan.api.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by sunnyLu on 2017/7/14.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CheckWithin {
    String value() default "no description";
}

在類上加註解

 

 

@CheckWithin
@Service("LoanServiceImpl")
public class LoanServiceImpl extends ServiceBase implements LoanService {

    @Override
    @CheckAop
    public BigDecimal testAop(TestParam testParam) {
        return null;
    }

}

切入點配置

 

 

package com.htt.app.loan.service.aop;

import org.aspectj.lang.annotation.*;

/**
 * Created by sunnyLu on 2017/6/13.
 */
@Aspect
public class AopCache {

    @Pointcut(value="@within(com.htt.app.loan.api.annotation.CheckWithin)")
    public void pointcut(){}

    @Before(value = "pointcut()")
    public void validateAnnotation() throws Throwable {
        System.out.println("註解式通知");
    }

}

匹配註解修飾的類的所有接口(這個類須要有public方法,否則不會被aop代理)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章