Spring基于AspectJ的AOP开发案例解析

 更新时间:2022年05月13日 11:03:38   作者:ClayBin  
这篇文章主要介绍了Spring的基于AspectJ的AOP开发,AspectJ是一个基于Java语言的AOP框架,使用AspectJ需要导入Spring AOP和AspectJ相关jar包,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
  • 使用AspectJ实现AOP
  • 注解方式
  • XML方式

AspectJ简介

  • AspectJ是一个基于Java语言的AOP框架
  • Spring2.0以后新增了对AspectJ切点表达式支持
  • @AspectJ是AspectJ1.5新增的功能,通过JDK5注解技术,允许直接在Bean类中定义切面
  • 新版本Spring框架,建议使用AspectJ方式来开发AOP
  • 使用AspectJ需要导入Spring AOP和AspectJ相关jar包
  • Spring-aop
  • springsource.org.aopalliance
  • spring-aspects
  • springsource.org.aspectj.weaver

注解开发

环境准备

  • 应入相关的
  • jar包junit,javax.annotation-api,spring-core,spring-beans,spring-context,spring-expression,aopalliance,spring-aop(Spring基础包)
  • aspectjweaver,spring-aspects(AspectJ使用的)
  • spring-test(测试使用)
  • XML引入相应配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
        <!--需要单独引入-->
        <aop:aspectj-autoproxy/>
</beans>

不同的通知类型

  • @Before前置通知,相当于BeforeAdvice
  • @AfterReturning后置通知,相当于AfterReturningAdvice
  • @Around环绕通知,相当于MethodInterceptor(可以阻止方法的进行,功能最强。列:事务管理)
  • @AfterThrowing异常抛出通知,相当于ThrowAdvice
  • @After最终final通知,不管是否异常,该通知都会执行
  • @DeclarwParents引介通知,相当于IntroductionInterceptor(不要求掌握)

最通知中通过value属性定义切点

  • 通过execution函数,可以定义切点的方法切入
  • 语法: --execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)(访问修饰符可以省略)
  • 列如

--匹配所有类public方法 execution(publice * * (..))---第一个*:任意返回值 第二个*:任意名称 (..):任意参数 相当于所有以public开头的方法加了一个前置通知的话都会执行 --匹配指定包下所有类方法 execution(* top.odliken.demo.(..)) 不包含子包 .:包 --execution(* top.odliken.demo..(..)) ..便是包、子酸包下所有类 --匹配指定类所有方法 execution( top.odlien.demo.UserService.(..)) 第一个*:任意返回值 UserService.:UserService类下面的所有方法 --匹配实现特定接口所有方法 execution( top.odliken.demo.GenericDao+.(..)) +:子类 .:方法名 --匹配所有save开头的方法 execution(* save*(..))

入门案列

============XML==========
<!--配置目标类=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
===========ProductDao===========
public class ProductDao {
    public void save() {
        System.out.println("保存商品.....");
    }
    public void findOne() {
        System.out.println("查找一个商品.....");
    }
    public void findAll() {
        System.out.println("查找所有商品.....");
    }
    public void update() {
        System.out.println("修改商品.....");
    }
    public void delete() {
        System.out.println("删除商品.....");
    }
}
===========MyAspectAnno===========
@Aspect
public class MyAspectAnno {
    @Before(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
    public void before(){
        System.out.println("=========前置通知=========");
    }
}
===========Springdemo1===========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:afterglow.xml")
public class Springdemo1 {
    @Resource(name = "productDao")
    private ProductDao productDao;
    @Test
    public void demo1(){
        productDao.save();
        productDao.findOne();
        productDao.findAll();
        productDao.update();
        productDao.delete();
    }
}

@Before前置通知

  • 可以在方法中传入JoinPoint对象,用来获取切点信息
public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}

@AfterReturning后置通知

  • 通过returning属性可以定义返回值,作为参数
@AfterReturning(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.update(..))",returning = "result")
public void afterReturing(Object result){
    System.out.println("==========后置通知=========="+result);
}

@Around环绕通知

  • Around方法的返回值就是目标代理方法执行返回值
  • 参数ProceedingJoinPoint 可以调用拦截目标方法执行
  • 如果不调用 ProceedingJoinPoint的 proceed方法,那么目标方法就背拦截了
@Around(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.delete(..)))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("环绕通知======前");
    Object obj = joinPoint.proceed();//执行目标方法 不调用就不执行
    System.out.println("环绕通知======后");
    return obj;
}

@AfterThrowing 异常抛出通知

  • 通过设置throwing属性,可以设置发生异常对象参数
@AfterThrowing(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findOne(..)))",throwing = "e")
public void  afterThrowing(Throwable e){
    System.out.println("==========异常通知=========="+e.getMessage());
}

@After 最终通知

  • 无论是否出现异常,最终通知总是会被执行的。就像Java异常中的 finall块一样
@After(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findAll(..))")
public void after(){
    System.out.println("==========最终通知==========");
}

通过@Pointcut为切点命名

  • 在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用@Pointcut进行定义
  • 切点方法:private void 无参方法,方法名为切点名
  • 当通知多个切点是,可以使用||连接
@Before(value = "myPointcut1()")
public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}
@Pointcut(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
private void myPointcut1(){}

AspectJ的XML方式的AOP开发

==========CustomerDao==========
public interface CustomerDao {
    public void save();
    public String update();
    public void delete();
    public void findOne();
    public void findAll();
}
==========CustomerDaoImpl==========
public class CustomerDaoImpl implements CustomerDao {
    public void save() {
        System.out.println("保存客户...");
    }
    public String update() {
        System.out.println("修改客户...");
        return "spring";
    }
    public void delete() {
        System.out.println("删除客户...");
    }
    public void findOne() {
        System.out.println("查询一个客户...");
//       int a = 1/ 0;
    }
    public void findAll() {
        System.out.println("查询多个客户...");
//        int b = 1/0;
    }
}
==========MyAspectXml==========
public class MyAspectXml {
    // 前置通知
    public void before(JoinPoint joinPoint) {
        System.out.println("XML方式的前置通知==============" + joinPoint);
    }
    // 后置通知
    public void afterReturing(Object result) {
        System.out.println("XML方式的后置通知==============" + result);
    }
    // 环绕通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("XML方式的环绕前通知==============");
        Object obj = joinPoint.proceed();
        System.out.println("XML方式的环绕后通知==============");
        return obj;
    }
    // 异常抛出通知
    public void afterThrowing(Throwable e) {
        System.out.println("XML方式的异常抛出通知=============" + e.getMessage());
    }
    // 最终通知
    public void after() {
        System.out.println("XML方式的最终通知=================");
    }
}
==========SpringDemo2==========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class SpringDemo2 {
    @Resource(name="customerDao")
    private CustomerDao customerDao;
    @Test
    public void demo1(){
        customerDao.save();
        customerDao.update();
        customerDao.delete();
        customerDao.findOne();
        customerDao.findAll();
    }
}
==========XML==========
<!--XML的配置方式完成AOP的开发===============-->
<!--配置目标类=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
<!--aop的相关配置=================-->
<aop:config>
    <!--配置切入点-->
    <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
    <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
    <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
    <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
    <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
    <!--配置AOP的切面-->
    <aop:aspect ref="myAspectXml">
        <!--配置前置通知-->
        <aop:before method="before" pointcut-ref="pointcut1"/>
        <!--配置后置通知-->
        <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
        <!--配置环绕通知-->
        <aop:around method="around" pointcut-ref="pointcut3"/>
        <!--配置异常抛出通知-->
        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
        <!--配置最终通知-->
        <aop:after method="after" pointcut-ref="pointcut5"/>
    </aop:aspect>
</aop:config>

到此这篇关于Spring的基于AspectJ的AOP开发的文章就介绍到这了,更多相关Spring基于AspectJ的AOP开发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringMVC自定义拦截器登录检测功能的实现代码

    SpringMVC自定义拦截器登录检测功能的实现代码

    这篇文章主要介绍了SpringMVC自定义拦截器登录检测功能的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • Java数据结构之对象的比较

    Java数据结构之对象的比较

    比较对象是面向对象编程语言的一个基本特征,下面这篇文章主要给大家介绍了关于Java数据结构之对象的比较,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • Springboot和Jpa实现学生CRUD操作代码实例

    Springboot和Jpa实现学生CRUD操作代码实例

    这篇文章主要介绍了Springboot和Jpa实现学生CRUD操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot整合OpenApi的实践

    SpringBoot整合OpenApi的实践

    本文主要介绍了SpringBoot整合OpenApi,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Java对类私有变量的暴力反射技术讲解

    Java对类私有变量的暴力反射技术讲解

    今天小编就为大家分享一篇关于Java对类私有变量的暴力反射技术讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Spring Boot实现模块化的几种方法

    Spring Boot实现模块化的几种方法

    模块可以是业务模块,为应用程序提供一些业务服务,或者为几个其他模块或整个应用程序提供跨领域关注的技术模块。这篇文章主要介绍了Spring Boot实现模块化,需要的朋友可以参考下
    2018-07-07
  • java 字段值为null,不返回该字段的问题

    java 字段值为null,不返回该字段的问题

    这篇文章主要介绍了java 字段值为null,不返回该字段的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 对Java ArrayList的自动扩容机制示例讲解

    对Java ArrayList的自动扩容机制示例讲解

    今天小编就为大家分享一篇对Java ArrayList的自动扩容机制示例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • 使用springboot 打包插件去除jar包瘦身

    使用springboot 打包插件去除jar包瘦身

    这篇文章主要介绍了使用springboot 打包插件去除jar包瘦身的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Mybatis实战教程之入门到精通(经典)

    Mybatis实战教程之入门到精通(经典)

    MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架,通过本文给大家介绍Mybatis实战教程之入门到精通,对mybatis实战教程相关知识感兴趣的朋友一起学习吧
    2016-01-01

最新评论