Spring 基于 AspectJ 的 AOP 开发

在 Spring 的 aop 代理方式中, AspectJ 才是主流。


1. AspectJ 简介

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

2. 注解开发:环境准备

<?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 --> </beans>

3. @Aspect 提供不同的通知类型

  • @Before 前置通知,相当于 BeforeAdvice
  • @AfterReturning 后置通知,相当于 AfterReturning
  • @Around 环绕通知,相当于 MethodInterceptor
  • @After Throwing 异常抛出通知,相当于 ThrowAdvice
  • After 最终通知,不管是否异常,改通知都会执行
  • DeclareParents 引介通知,相当于 IntroductionInterceptor (不要求掌握)
a. @Before 前置通知

可以在方法中传入 JoinPoint 对象,用来获得切点信息

// 要增强的代码
@Before(value = "execution(* com.test.aspectJ.demo1.ProductDao.save(..))")
public void before(JoinPoint joinPoint) {
System.out.println("前置通知=========" + joinPoint);
}
b. @AfterReturning 后置通知

通过 returning 属性,可以定义方法返回值,作为参数:

// result 拿到返回值
@AfterReturning(value = "execution(* com.test.aspectJ.demo1.ProductDao.update(..))", returning = "result")
public void afterReturning(Object result) {
System.out.println("后置通知=========="+result);
}
c. @Around 环绕通知
  • around 方法的返回值就是目标代理方法执行返回值
  • 参数为 ProceedingJoinPoint 可以调用拦截目标方法执行
@Around(value = "execution(* com.test.aspectJ.demo1.ProductDao.delete(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前通知============");
Object obj = joinPoint.proceed(); //执行目标方法
System.out.println("环绕后通知==========");
return obj;
}

重点:如果不调用 ProceedingJoinPoint 的 proceed 方法,那么目标方法就被拦截了。

d. @AfterThrowing 异常抛出通知

通过设置 throwing 属性,可以设置发生异常对象参数

@AfterThrowing(value = "execution(* com.test.aspectJ.demo1.ProductDao.find(..))", throwing = "e")
public void afterThrowing(Throwable e) {
System.out.println("异常抛出通知==========="+e.getMessage());
}
e. After 最终通知

无论是否出现异常,最终通知总是会被执行

@AfterThrowing(value = "execution(* com.test.aspectJ.demo1.ProductDao.findAll(..))", throwing = "e")
public void afterThrowing(Throwable e) {
System.out.println("异常抛出通知==========="+e.getMessage());
}

4. 在通知中通过 value 属性定义切点

通过 execution 函数,可以定义切点的方式切入

语法:

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

例如:

  • 匹配所有类 public 方法:

    execution(public * * (..))
  • 匹配指定包下所有类方法:

    execution(* com.test.dao.*(.))    //不包含子包
    execuiton(* com.test.dao..*(..)) // ..* 表示包,子孙包下所有类
  • 匹配指定类所有方法:

    execution(* com.atest.service.UserService.*(..))
  • 匹配实现特定接口所有类方法:

    execution(* com.test.doa.GenericDao + .*(..))
  • 匹配所有 save 开头的方法:

    execution(* save*(..))

5. 为目标类定义切面类

定义切面类:

@Aspect
public class MyAspectAnno {}

6. 通过 @Pointcut 为切点命名

  • 在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用 @Point 进行定义
  • 切点方法:private void 无参数方法,方法名为切点名
  • 当通知多个切点时,可以使用 || 进行连接
@Pointcut(value = "execution(* com.test.aspectJ.demo1.ProductDao.save(..))")
private void myPointcut1() {}

具体代码:SpringDemo1.demo1()


基于 AsepctJ 的 XML 方法的 AOP 开发

1. 编写切面类

public class MyAspectXml {

    //前置通知
public void before(JoinPoint joinPoint) {
System.out.println("XML方法的前置通知=========="+joinPoint);
} // 后置通知
public void afterReturning(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方法的最终通知");
} }

2. 完成切面类的配置

<!-- 配置切面类 -->
<bean id="myAspectXml" class="com.test.aspectJ.demo2.MyAspectXml" />

3. 配置 AOP 完成增强

<!-- aop 的相关配置 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="pointcut1" expression="execution(* com.test.aspectJ.demo2.CustomerDaoImpl.save())" />
<aop:pointcut id="pointcut2" expression="execution(* com.test.aspectJ.demo2.CustomerDaoImpl.update())" />
<aop:pointcut id="pointcut3" expression="execution(* com.test.aspectJ.demo2.CustomerDaoImpl.delete())" />
<aop:pointcut id="pointcut4" expression="execution(* com.test.aspectJ.demo2.CustomerDaoImpl.findOne())" />
<aop:pointcut id="pointcut5" expression="execution(* com.test.aspectJ.demo2.CustomerDaoImpl.findAll())" />
<!-- 配置AOP的切面 -->
<aop:aspect ref="myAspectXml">
<!-- 配置前置通知 -->
<aop:before method="before" pointcut-ref="pointcut1" />
<!-- 配置后置通知 -->
<aop:after-returning method="afterReturning" 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>

具体代码:SpringDemo2.demo1()

Spring 基于 AspectJ 的 AOP 开发的更多相关文章

  1. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  2. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  3. Spring框架学习09——基于AspectJ的AOP开发

    1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...

  4. Spring基于AspectJ的AOP的开发之AOP的相关术语

    1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...

  5. 十四 Spring的AOP的基于AspectJ的注解开发

    Spring的AOP的基于AspectJ的注解开发 创建项目,引入jar包 编写目标类.切面类 配置目标类.切面类 在注解文件里开启AOP的开发 <?xml version="1.0& ...

  6. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  7. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  8. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...

  9. Spring_AOP基于AspectJ的注解开发&JDBC的模板使用&事务管理(学习笔记3)

    一:AOP基于AspectJ的注解开发 1,简单的实例: 1)引入相应的jar包 ​ 2)在配置文件里引入相关约束 <beans xmlns="http://www.springfra ...

随机推荐

  1. SPOJ AMR12B 720

    这个题应该是个优先队列的模版题 当时比赛的时候没时间做先贴一下大神的代码好好学习学习 B - Gandalf vs the Balrog Time Limit:2000MS     Memory Li ...

  2. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  3. HTTP中GET请求与POST请求的区别

    GET和POST是HTTP请求的两种基本方法.最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数.但这只是表象,或者说只是现象,并不是核心.本文将细细谈谈对它们的 ...

  4. gitlib的安装

    下载ruby yum -y install gcc gcc-c++ make wget https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.9.tar. ...

  5. [go]包管理

    vendor方式 //包管理发展 go get(无版本概念) -> vendor(godep)(无版本概念) -> go modules go get github.com/tools/g ...

  6. 【Taro全实践】6位验证码输入视觉分离(标准下划线分离)

    一.实现的效果图 二.实现思路 中间想过很多实现方法,但是因为input为原生组件的原因,很难适配所有手机直接. 所有如何实现适配所有手机的验证码分离输入呢?(思路如下) 1.input组件为原生组件 ...

  7. SEO中常用的301永久重定向代码大全

    301是永久重定向的意思,表示请求的网页已永久移动到新位置,服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置.其实301重定向在SEO中被广泛应用,也是被广泛认为比 ...

  8. Mybatis 联合查询XML与注解对比

    由于是练习,故只做了感兴趣的一部分测试. 测试类容XML配置转注解方式 实体类为了测试请忽略设计是否合理… User.java @Alias("User")public class ...

  9. git合并时忽略某个文件

    因为开发现场跟部署的环境不同,有很多ip地址每次都要改来改去;于是开两个分支master(用来保存部署现场的ip)和dev(开发环境的ip),开发功能时在dev分支,然后使用master合并,每个分支 ...

  10. MariaDB基本知识点总结01--介绍+语句

    一.概念 1.数据库介绍: 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理数据 ...