spring AOP pointcut 详细用法

1.首先这是我几天来对切面的编程的理解,之前有稍微学了下 spring切面的编程,spring中事物处理常常与pointcut相结合。

pointcut的注解类型 表达式 我就不多说了 。具体可以看spring文档的第199页~200页,都比较简单。


大体上是这样的 注解 + (表达标签+表达式格式)

如: @Pointcut (value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")

       @ 注解( value=“ 表达标签 ( 表达式格式)”)

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class NotVeryUsefulAspect {
@AfterReturning(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private void logReceiver(){
System.out.println("切入点logReceiver...");
}

@Pointcut(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..)) && args(param)")
private void pointcut(String param){ 
System.out.println("切入点pointcut()"+param);
}

//方法体将不执行
@Pointcut("within(com.cn.spring.aspectj.*)")
public String inWebLayer() {
System.out.println("切入点inWebLayer()");
return "返回值加载";
}

@Before(value="inWebLayer()")
private void beforeinWebLayer(){ 
System.out.println("beforeinWebLayer~~");
}

@Before(value="pointcut(param)")
private void beforePointcut(String param){ 
System.out.println("beforePointcut:"+param);
}


@AfterReturning(pointcut="inWebLayer()",returning="retVal")
public void doAccessCheck(Object retVal) {
System.out.println("doAccessCheck:"+retVal);
}

@Around(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private Object aroundLayer(ProceedingJoinPoint pjp) throws Throwable{ 
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
System.out.println("aroundLayer~~");
return retVal;
}
}

以上声明需要注意:spring基本包不提供Aspect包,需要另外引入,可以去eclipse官网下载:http://www.eclipse.org/aspectj/

注意 申明@Aspect 需要 引入该bean  否则 spring将不识别。如上@Component或者xml引入

表达式中拦截了com.cn.spring.aspectj.NotVeryUsefulAspectService该类中所有方法。当执行行该类中方法时 执行相应的拦截方法,pointcut只负责 切入方法,并未执行方法体。这点要注意!

此后 讲解下Aspect  几个通知注解(advice) 

@Pointcut 拦截的切入点方法,注解的在方法级别之上,但是不执行方法体,只表示切入点的入口。

@Before 顾名思义 是在 切入点 之前执行 方法。

@AfterReturning 返回拦截方法的返回值 

@AfterThrowing 拦截的方法 如果抛出异常 加执行此方法 throwing="ex" 将异常返回到参数列表

@After 在之上方法执行后执行结束操作


@Around  判断是否执行 以上的拦截 ,第一个参数必须ProceedingJoinPoint. 如要拦截: 


@Around(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private Object aroundLayer(ProceedingJoinPoint pjp) throws Throwable{ 
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
System.out.println("aroundLayer~~");
return retVal;
}

就代表 需要执行之后的拦截 ,此拦截 在@Before 之前 做逻辑判断。


  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值