Spring Aop织入点语法和相关案例总结

x33g5p2x  于2021-03-14 发布在 Spring  
字(1.2k)|赞(0)|评价(0)|浏览(277)

Aspectj织入点语法:

1、execution(public * *(..))   任何类的任何返回值的任何方法

2、execution(* set*(..))       任何类的set开头的方法

3、execution(* com.xyz.service.AccountService.*(..))         任何返回值的规定类里面的方法

4、execution(* com.xyz.service...(..))      任何返回值的,规定包或者规定包子包的任何类任何方法

Advise总结。举例说明:

1、举例:直接指定要织入的位置和逻辑

//指定织入的方法。
	@Before("execution(public * com.spring.service..*.*(..))")
	public void BeforeMethod(){
		System.out.println("method start!");
	}
	
	
	@AfterReturning("execution(public * com.spring.service..*.*(..))")
	public void AfterMethod(){
		System.out.println("After returnning");
	}

2、通过定义pointcut来指定:

//定义pointcut织入点集合
	@Pointcut("execution(public * com.spring.service..*.*(..))")
	public void MyMethod(){}
	
	@Before("MyMethod()")
	public void BeforeMethod(){
		System.out.println("method start!");
	}
	
	@AfterReturning("MyMethod()")
	public void AfterMethod(){
		System.out.println("After returnning");
	}
	
	//执行前后都拦截。以pjp.proceed的方法分割开来
	@Around("MyMethod()")
	public void aroundProcced(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("around start");
		pjp.proceed();
		System.out.println("around end");
	}

输出结果:

method start!
around start
helloworld
After returnning
around end

相关文章