Spring入门Blog[八、Spring Aop织入点语法和相关案例总结]

本文介绍了AspectJ中织入点的基本语法,并通过实例展示了如何使用@Before、@AfterReturning及@Around注解来实现方法调用前后的增强逻辑。同时,文章还详细解释了如何通过定义pointcut来灵活地指定切面的应用范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值