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)。