Spring AOP用法(方法理解)

    Spring AOP用法(方法理解)

在使用AOP处理数据源的时候,对其方法进行了一顿所谓的复习。。。

直接上代码:

@Aspect
@Component
public class CxAspect {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Pointcut("@annotation(com.cx.annotation.DataSource)")
	public void cxPointcut(){
		System.out.println("执行切点---cxPointcut");
	}
	
	@Around("cxPointcut()")
	public Object cxAround(ProceedingJoinPoint point) throws Throwable{
		System.out.println("执行进入cxAround方法");
		Object data =  point.proceed();
		System.out.println("cxAround--proceed执行完成!");
		return data;
	}
	
	@Before("cxPointcut()")
	public void cxBefore(){
		System.out.println("执行cxbefore方法");
	}
	
	@After("cxPointcut()")
	public void cxAfter(){
		System.out.println("执行cxAfter方法");
	}
}

其中针对切点cxPointcut方法,是不会进入执行方法体的;所有操作都是围绕此点进行。切点的过滤条件是作用在注解上的,

使用了自定义注解@DataSource,凡是加了此注解的方法,都会使aop发生作用。

表达式:

("@annotation(com.cx.annotation.DataSource)")

@Before,在此点之前执行方法体;

@After ,在此点之后执行方法体;

@Around,围绕此点执行,执行前-点-执行后,proceed之前是点执行前,proceed之后是点执行后的操作。记得return proceed的Object对象,否则执行点方法返回的内容将丢失(null)。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值