当前位置:   article > 正文

spring aop 注解_spring aop注解

spring aop注解

spring Aop 注解

Aop 术语

AOP 的特性术语,不同的翻译还会不一样,得在过程中理解

  • 横切关注点:跨越程序多个模块的方法或功能。即与业务逻辑无关,但我们也要关注的部分,就是横切关注点。如日志、安全、缓存等。
  • 切面( Aspect ):横切关注点被模块化的特殊对象;即切面应是一个类。
  • 通知( Advice):切面要完成的增强处理,通知描述了切面何时执行以及如何执行增强处理;即通知应是切面中的方法。
  • 目标( Target ):被通知对象。
  • 代理( Proxy ):向目标对象应用通知之后创建的对象。
  • 切入点( PointCut ):切面通知执行的地点,即可以插入增强处理的连接点。
  • 连接点( JoinPoint ):应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。
  • 织入( Weaving ):将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入

Spring Aop 通知

Spring中有五种通知 Advice

  • @Before 前置通知:方法执行之前
  • @After 后置通知:方法执行之后
  • @Around 环绕通知:方法执行前后都有通知
  • @AfterReturnning 返回通知:方法成功执行之后通知
  • @AfterThrowing 异常通知:抛出异常之后通知

举例

加入依赖
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aop</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.6</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
业务类
@Component
public class User {


    public void sayHi(String name) {
        System.out.println(">>> user hi: " + name);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
定义切面
@Aspect
@Component
public class UserAspect {

    @Pointcut("execution(public * com.x.z..*.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("before advice...");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("after advice...");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around advice before...");
        Object proceed = pjp.proceed();
        System.out.println("around advice after...");
        return proceed;
    }

    @AfterReturning(value = "pointCut()")
    public void afterReturning() {
        System.out.println("AfterReturning advice...");
    }

    @AfterThrowing(value = "pointCut()")
    public void afterThrowing() {
        System.out.println("AfterThrowing advice...");
    }

}

@Configuration
@EnableAspectJAutoProxy   //一定加的注解, 不然切面不生效
@Import({UserAspect.class, User.class})
public class UserConfig {


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
AnnotationConfigApplicationContext 加载切面
public class UserAopTest {


    @Test
    public void userAspectTest(){
        ApplicationContext act = new AnnotationConfigApplicationContext(UserConfig.class);
        User user = act.getBean(User.class);
        user.sayHi("小明");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
结果
around advice before...
before advice...
>>> user hi: 小明
AfterReturning advice...
after advice...
around advice after...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Aop执行切面源码

在这里插入图片描述

很明显,我这个是类代理,所以进入 DynamicAdvisedInterceptor

在这里插入图片描述

获取增强链:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

aop 流程:

  • DynamicAdvisedInterceptor.intercept()
  • 获取目标对象 TargetSource targetSource = this.advised.getTargetSource()
  • 获取通知链 List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass)
  • 递归调用 通知增强
  • 执行目标方法

AOP 原理

一句话: 生成代理对象,使用责任链式方式调用通知链执行增强方式,最后执行目标方法,达到增强目的

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/647724
推荐阅读
相关标签
  

闽ICP备14008679号