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.within:any 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代理)