19Spring_AOP编程(AspectJ)_使用@Pointcut注解来定义切点


我们之前的方式是采用

@AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
public void afterReturning(JoinPoint joinPoint,Object returnVal)
{
System.out.println("this is afterReturning 方法");
System.out.println(returnVal);
    
}

这样的方式。这样方式的话切点和通知是紧密联系在一起的,我们换另外一种方式。使用@Pointcut注解来定义切点。把两者解开。

案例如下:

//第二步定义一个通知,用上切点
//这里写的就是定义的那个用@Pointcut注解修饰的方法。
@After("mypoint()") public void after() { System.out.print("最终通知,释放资源"); }
//第一步:定义一个切点 @Pointcut(
"execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
//必须是private void 无参数
private void mypoint() { }

效果和之前的一模一样。

 

 

扩展:有没有我再想拦截一个切点怎么办?上面我已经有一个切点了,那么能不能一个advice有多个切点。答案是:当然可以

在开发中有这么种需求,即要切DAO层那边,也要切Server那边。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM