我们之前的方式是采用

@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那边。

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

 

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2021-05-24
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
相关资源
相似解决方案