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

目录
  • AspectJ简介
  • 注解开发
  • 环境准备
  • 不同的通知类型
    • 最通知中通过value属性定义切点
    • 入门案列
  • @Before前置通知
  • @AfterReturning后置通知
  • @Around环绕通知
  • @AfterThrowing 异常抛出通知
  • @After 最终通知
  • 通过@Pointcut为切点命名
  • AspectJ的XML方式的AOP开发
  • 使用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开发内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot开发教程之AOP日志处理

    目录 日志处理: 需求分析 实现过程: 实验效果: 参考文献: 总结 日志处理: 需求分析 日志处理需要记录的是: 请求的URL 访问者IP 调用的方法 传入的参数 返回的内容 上面的内容要求在控制台和日志中输出. 在学习这部分知识的时候,真的感觉收获很多,在之前Spring学习的aop只是初步了解,现在有了一些深入的理解.好记性不如烂笔头! 在日志处理这部分主要是aop的使用,通过切面的方式来整合到项目了,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率. 人话

  • Aspectj与Spring AOP的对比分析

    1.简介 今天有多个可用的 AOP 库, 它们需要能够回答许多问题: 1.是否与用户现有的或新的应用程序兼容? 2.在哪里可以实现 AOP? 3.与自己的应用程序集成多快? 4.性能开销是多少? 在本文中, 我们将研究如何回答这些问题, 并介绍 Spring aop 和 AspectJ, 这是 Java 的两个最受欢迎的 aop 框架. 2.AOP概念 在开始之前, 让我们对术语和核心概念进行快速.高层次的审查: Aspect -- 一种标准代码/功能, 分散在应用程序中的多个位置, 通常与实际

  • spring中AOP 注解开发示例详解

    一.简介 AOP主要包含了通知.切点和连接点等术语,介绍如下: 通知(advice) 通知定义了切面是什么以及何时调用,何时调用包含以下几种 Before 在方法被调用之前调用通知 After 在方法完成之后调用通知,无论方法执行是否成功 After-returning 在方法成功执行之后调用通知 After-throwing 在方法抛出异常后调用通知 Around 通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为 切点(PointCut) 通知定义了切面的什么和何时,切

  • SpringBoot开发技巧之使用AOP记录日志示例解析

    目录 为什么要用AOP? 常用的工作场景 必须知道的概念 AOP 的相关术语 Spring 中使用注解创建切面 实战应用-利用AOP记录日志 定义日志信息封装 统一日志处理切面 为什么要用AOP? 答案是解耦! Aspect Oriented Programming 面向切面编程.解耦是程序员编码开发过程中一直追求的.AOP也是为了解耦所诞生. 具体思想是:定义一个切面,在切面的纵向定义处理方法,处理完成之后,回到横向业务流. AOP 主要是利用代理模式的技术来实现的.具体的代理实现可以参考这篇

  • Spring用AspectJ开发AOP(基于Annotation)

    基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. 为此,AspectJ 框架为 AOP 开发提供了另一种开发方式--基于 Annotation 的声明式.AspectJ 允许使用注解定义切面.切入点和增强处理,而 Spring 框架则可以识别并根据这些注解生成 AOP 代理. 关于 Annotation 注解的介绍如表 1

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

    目录 AspectJ简介 注解开发 环境准备 不同的通知类型 最通知中通过value属性定义切点 入门案列 @Before前置通知 @AfterReturning后置通知 @Around环绕通知 @AfterThrowing 异常抛出通知 @After 最终通知 通过@Pointcut为切点命名 AspectJ的XML方式的AOP开发 使用AspectJ实现AOP 注解方式 XML方式 AspectJ简介 AspectJ是一个基于Java语言的AOP框架 Spring2.0以后新增了对Aspec

  • 浅析Spring基于注解的AOP

    目录 一.准备工作 二.基于注解的AOP之前置通知 三.基于注解的AOP之切入点表达式的语法和重用以及获取连接点的信息 ①切入点表达式的语法 ②获取连接点的信息 ③重用写入点表达式 一.准备工作 ①创建一个Maven工程 ②添加依赖 在IOC所需依赖基础上再加入下面依赖即可: <!-- spring-aspects会帮我们传递过来aspectjweaver --> <dependency> <groupId>org.springframework</groupId

  • SpringCloud基于RestTemplate微服务项目案例解析

    目录 基于RestTemplate微服务项目 一.构建父工程 二.构建serverspringcloud-api(公共子模块) 三.创建部门微服务提供者 四.创建部门微服务消费者 五.总结 基于RestTemplate微服务项目 在写SpringCloud搭建微服务之前,我想先搭建一个不通过springcloud只通过SpringBoot和Mybatis进行模块之间额通讯.然后在此基础上再添加SpringCloud框架. 下面先对案例做个说明 该项目有一个maven父模块,其中里面有三个子模块:

  • Spring基于advisor配置aop过程解析

    1.目标类 package com.gec.target; public class Hadoop { public void eatting() { System.out.println("大象正在吃东西 1"); try { //耗时5秒 Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } 2.增强类,此类必须要实现增强方位接口 package com.gec.advic

  • Spring基于ProxyFactoryBean创建AOP代理

    Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类方法的连接点位置,可以分为以下五种类型,如表 1 所示. 表 1 Spring 通知的 5 种类型 名称 说明 org.springframework.aop.MethodBeforeAdvice(前置通知) 在方法之前自动执行的通知称为前置通知,可以

  • Spring基于XML实现Aop

    目录 项目结构 具体步骤 1.创建maven 项目 导入依赖 创建好项目结构 2.写一个TestDao接口 及实现类 3.编写切面类 测试 总结 项目结构 具体步骤 1.创建maven 项目 导入依赖 创建好项目结构 <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version&g

  • 带你了解如何使用Spring基于ProxyFactoryBean创建AOP代理

    目录 1 基础 2 JavaBean属性 3 JDK和CGLIB代理 总结 若使用 Spring IoC 容器(ApplicationContext或BeanFactory)作为你的业务对象(你也应该这么做!),你会想使用 Spring AOP FactoryBean的一种. 工厂 bean 引入了中间层,让它创建不同类型的对象. 在Spring创建 AOP 代理的基本方式是使用 org.springframework.aop.framework.ProxyFactoryBean.这可以完全控制

  • Spring中基于xml的AOP的详细步骤

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点,也是Spring框架中一个重要的内容.是函数式编程的一个衍生范例,利用Aop可以对业务逻辑各个部分进行分割,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用行,提高了开发效率.简单的说就是把我们程序中的重复代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上已有的方法进

  • Spring使用AspectJ的注解式实现AOP面向切面编程

    1.认识Spring AOP 1.1 AOP的简介 AOP:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,而且类的继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足. 1.2 AOP中的概念 切入点(pointcut): 切入点(pointcut):在哪些类.哪些方法上切入. 通知(advice):在方法前.方法后.方法前后做什么. 切面(aspe

  • 实例讲解Java的Spring框架中的AOP实现

    简介 面向切面编程(AOP)提供另外一种角度来思考程序结构,通过这种方式弥补了面向对象编程(OOP)的不足. 除了类(classes)以外,AOP提供了 切面.切面对关注点进行模块化,例如横切多个类型和对象的事务管理. (这些关注点术语通常称作 横切(crosscutting) 关注点.) Spring的一个关键的组件就是 AOP框架. 尽管如此,Spring IoC容器并不依赖于AOP,这意味着你可以自由选择是否使用AOP,AOP提供强大的中间件解决方案,这使得Spring IoC容器更加完善

随机推荐