iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring基于AspectJ的AOP开发案例解析
  • 169
分享到

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

2024-04-02 19:04:59 169人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录AspectJ简介注解开发环境准备不同的通知类型最通知中通过value属性定义切点入门案列@Before前置通知@AfterReturning后置通知@Around环绕通知@Af

  • 使用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开发内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring基于AspectJ的AOP开发案例解析

本文链接: https://www.lsjlt.com/news/148708.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring基于AspectJ的AOP开发案例解析
    目录AspectJ简介注解开发环境准备不同的通知类型最通知中通过value属性定义切点入门案列@Before前置通知@AfterReturning后置通知@Around环绕通知@Af...
    99+
    2024-04-02
  • 浅析Spring基于注解的AOP
    目录一、准备工作二、基于注解的AOP之前置通知三、基于注解的AOP之切入点表达式的语法和重用以及获取连接点的信息①切入点表达式的语法②获取连接点的信息③重用写入点表达式一、准备工作 ...
    99+
    2022-11-13
    Spring AOP Spring基于注解的AOP
  • 基于spring中的aop简单实例讲解
    aop,即面向切面编程,面向切面编程的目标就是分离关注点,比如:一个骑士只需要关注守护安全,或者远征,而骑士辉煌一生的事迹由谁来记录和歌颂呢,当然不会是自己了,这个完全可以由诗人去歌颂,比如当骑士出征的时候诗人可以去欢送,当骑士英勇牺牲的时...
    99+
    2023-05-31
    spring aop 实例
  • Spring MVC注解式开发案例分析
    这篇文章主要讲解了“Spring MVC注解式开发案例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring MVC注解式开发案例分析”吧!项目案例用 Reque...
    99+
    2023-07-05
  • Java Spring之基于注解的AOP怎么配置
    本篇内容主要讲解“Java Spring之基于注解的AOP怎么配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java Spring之基于注解的AOP怎么配置”吧!1 环境搭建1.1 第一步:...
    99+
    2023-07-05
  • Spring框架基于注解开发CRUD详解
    Spring框架基于注解开发CRUD,供大家参考,具体内容如下 1. Maven坐标 <!-- https://mvnrepository.com/artifact/org.s...
    99+
    2024-04-02
  • Java之Spring注解开发案例详解
    在Spring4之后,要使用注解开发,必须要保证aop的包导入了 使用注解需要导入context约束,增加注解的支持! <?xml ver...
    99+
    2024-04-02
  • SpringCloud基于RestTemplate微服务项目案例解析
    目录基于RestTemplate微服务项目一、构建父工程二、构建serverspringcloud-api(公共子模块)三、创建部门微服务提供者四、创建部门微服务消费者五、总结基于R...
    99+
    2024-04-02
  • web开发中基于匀速运动的示例分析
    这篇文章主要介绍web开发中基于匀速运动的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!javascript中,如何让一个元素(比如div)运动起来呢?设置基本的样式,一定要...
    99+
    2024-04-02
  • 基于uni-app开发刻度尺组件的示例分析
    这篇文章将为大家详细讲解有关基于uni-app开发刻度尺组件的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、前言小编最近接到一个任务,就是在输入数值的时候不再使用传统的键盘了,而是用拖拉尺子的...
    99+
    2023-06-29
  • Spring切面优先级与基于xml的AOP实现方法详解
    目录一、切面的优先级二、基于xml的AOP实现(了解)一、切面的优先级 ①创建类ValidateAspect: 由于要把我们的切面类和我们的目标类来进行ioc容器的一个组件,所以我们...
    99+
    2022-11-13
    Spring切面优先级 Spring XML实现Aop
  • 利用Spring Boot开发REST服务的案例
    这篇文章主要介绍了利用Spring Boot开发REST服务的案例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。REST服务介绍RESTful service是一种架构模式,...
    99+
    2023-05-30
    springboot rest
  • SpringBoot开发技巧之使用AOP记录日志示例解析
    目录为什么要用AOP?常用的工作场景必须知道的概念AOP 的相关术语Spring 中使用注解创建切面实战应用-利用AOP记录日志定义日志信息封装统一日志处理切面为什么要用AOP? 答...
    99+
    2024-04-02
  • Spring Boot应用开发的示例分析
    这篇文章主要介绍了Spring Boot应用开发的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Spring Boot是由Pivotal团队提供的全新Spring开发...
    99+
    2023-06-20
  • spring boot基于注解声明式事务配置的示例分析
    小编给大家分享一下spring boot基于注解声明式事务配置的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!事务配置1、配置方式一1)开启spring事务管理,在spring boot启动类添加注解@Enable...
    99+
    2023-06-20
  • Java Spring-IOC容器与Bean管理之基于注解的方式案例详解
    Spring-IOC容器-Bean管理-基于注解方式 什么是注解? (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…) (2)使用注解,注解作用在类...
    99+
    2024-04-02
  • 基于spring注入为null的原因及解决方案
    目录spring注入为null场景经检查找出原因解决方法@PostConstruct:Spring注入的属性为null可能的情况保证Bean能够交付给Spring容器检查实例化的方式...
    99+
    2024-04-02
  • 基于java的opencv开发过程详解
    1.下载安装OpenCV https://opencv.org/releases/ 选择合适的平台安装包下载,然后双击安装,也就是解压的过程。这里主要记录windows下的环境搭建...
    99+
    2024-04-02
  • 基于Redis结合SpringBoot的秒杀案例详解
    目录1、构建SpringBoot项目2、启动类3、在Controller层里定义秒杀接口4、在Service层里通过lua脚本实现秒杀效果5、配置redis连接参数6、演示秒杀效果&...
    99+
    2024-04-02
  • 基于Koa2开发微信二维码扫码支付的示例分析
    这篇文章主要介绍基于Koa2开发微信二维码扫码支付的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!两种模式打开微信支付的文档,我们可以看到两种支付模式:模式一和模式二。这二者...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作