SpringAOP使用总结

一、AOP切面的方式

1.很多时候我们需要对一层进行切面,例如我们要对系统的Controler层进行切面,但是关于登陆和注册的Controller则放开;书写方式如下,切Service同理:以下表达方式涵盖了包名、类名切面方法;

 //所有以controller下面的请求都要经过校验 排除掉登录
    @Pointcut("execution(public * cn.czyfwpla.xm.controller..*.*(..))"+
            "&& !execution(public * cn.czyfwpla.xm.controller.LoginController.*(..))")
    public void vertify(){}

2.有些时候我们可能只需要对特定的方法进行切面,这个时候我们不会管他在哪一层,则采用自定义切面的方式进行:

1)自定义切面

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @ interface Page {
    int pageSize() default 10;
}

2)Aop切面书写:

 @Pointcut("@annotation(cn.czyfwpla.sys.annotation.Page)")
    public void onAspect() {
}

以上两种切面方式个人觉得为常用的切面方式,自定义注解的切面方式可以解决一些特殊切面的问题。当然切面的内容还有很多,这里就只总结了个人觉得比较实用的几种。

二、AOP的拦截

1.异常情况  @AfterThrowing(pointcut = "onAspect()",throwing = "e")

2.正常返回结果 @AfterReturning(value = "onAspect()", returning = "returnVal")

3.切面之前进行操作 @Before

4.切面之后进行操作 @After

5.@Around 待续…

发布了53 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Min_Chander/article/details/101289439