栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@AspectJ中的几种通知方式详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@AspectJ中的几种通知方式详解

本文来详细说下@AspectJ中的几种通知方式

文章目录
  • 概述
  • 通知方式说明
  • 一个例子
  • 本文小结


概述

当Spring 2.0发布以后,Spring AOP增加了新的使用方式,Spring AOP集成了AspectJ。我们最常用的就是这个版本的Spring AOP。

主要有如下变化

  • 可以用POJO来定义Aspect和Adivce,并提供了一系列相应的注解,如@Aspect和@Around等。而不用像1.x版本中实现相应的接口
  • 支持aspectj中的pointcut的表达方式,我们都深有体会哈


通知方式说明

下满这个是官网对@AspectJ中的几种通知方式的说明


一个例子

导入@AspectJ注解的maven

 
    org.aspectj
    aspectjweaver
    1.9.7
 

选择连接点,就是业务方法,可以看作动态代理中的目标对象

Spring 是方法级别的 AOP 框架,我们主要也是以某个类额某个方法作为连接点,另一种说法就是:选择哪一个类的哪一方法用以增强功能。

package cn.wideth.buz.test;

public class Landlord {

    public void service() {

        // 仅仅只是实现了核心的业务功能
        System.out.println("业务方法 => service()");
//        throw new RuntimeException();

    }
}

我们在这里就选择上述 Landlord 类中的 service() 方法作为连接点。

创建切面

选择好了连接点就可以创建切面了,我们可以把切面理解为一个拦截器,当程序运行到连接点的时候,被拦截下来,在开头加入了初始化的方法,在结尾也加入了销毁的方法而已,在 Spring 中只要使用 @Aspect 注解一个类,那么 Spring IoC 容器就会认为这是一个切面了。

package cn.wideth.buz.test;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;


@Aspect
public class Broker {

    
    @Pointcut("execution(* cn.wideth.buz.test.Landlord.service(..))")
    public void lService() {
    }

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

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

    
    @Around("lService()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("<== around");
        Object result = joinPoint.proceed();
        System.out.println("around ==>");
        return result;
    }

    
    @AfterReturning("lService()")
    public void afterReturning(){
        System.out.println("AfterReturning()");
    }

    
    @AfterThrowing("lService()")
    public void afterThrowing(){
        System.out.println("afterThrowing()");
    }

}

启用AOP

因为使用注解的方式来操作AOP,所以编写一个配置类来开启AOP

package cn.wideth.buz.test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

// 启用AOP
@EnableAspectJAutoProxy
public class AspectJConfig {

    @Bean
    public Landlord getLandlord() {
        return new Landlord();
    }
}

测试 AOP

package cn.wideth.buz.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AspectJConfig.class, Broker.class);

        Landlord landlord = context.getBean(Landlord.class);
        landlord.service();
        context.close();

    }
}

测试结果

Adivce之间的顺序关系

正确的情况下

@Around->@Before->方法执行->@AfterReturning ->@After-> @Around


本文小结

本文介绍了@AspectJ中的几种通知方式,以及5种通知的先后顺序。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325442.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号