Spring提供的AOP方式

@Component
@Aspect
public class AopAspect {@Pointcut(value = "execution(* com.ztryou.service.PersonServiceImpl.*(..))")public void pointcut() {}@Before("pointcut()")public void beforeAdvice(JoinPoint point) throws Exception {System.out.println("befor");}@Around(value = "pointcut()")public Object aroundAdvice(ProceedingJoinPoint pjp) {Object obj = null;try {System.out.println("around before");MethodSignature ms = (MethodSignature) pjp.getSignature();Object target = pjp.getTarget();Method method = ms.getMethod();method.invoke(target, 1);obj = pjp.proceed();System.out.println("around after");} catch (Throwable e) {e.printStackTrace();}return obj;}/*** 返回后通知:在连接点正常执行完后执行的通知*/@AfterReturning(value = "pointcut()", returning = "ret")public void afterReturningAdvice(Object ret) {System.out.println("after return");}/*** 抛出异常后通知:在连接节点抛出异常退出时执行的通知*/@AfterThrowing(value = "pointcut()", throwing = "ex")public void afterThrowingAdvice(Exception ex) {System.out.println("after Throw");}@After("pointcut()")public void afterFinallyAdvice() {System.out.println("after");}}

SpringAOP的底层实现

主要设计三个方面:

  1. advisor:切面,切面则包含下面的切点和拦截器
public interface Advisor {Advice getAdvice();。。。。。
}public interface PointcutAdvisor extends Advisor {Pointcut getPointcut();}
  1. pointcut: 切点
public interface Pointcut {// 用来匹配类ClassFilter getClassFilter();// 更细粒度的匹配,匹配方法MethodMatcher getMethodMatcher();}
  1. advice:增强(拦截器,用于执行自己的逻辑)
public interface Advice {}

这里注意,增强只是一个标识接口,用来表示这是一个增强类。我们要增强的逻辑是要实现MethodInterceptor接口,在invoke方法中写的。

public interface MethodInterceptor extends Interceptor {Object invoke(MethodInvocation invocation) throws Throwable;
}

每个都有自己的接口,只需要自己实现子类。

Spring提供的现成的类

RegexpMethodPointcutAdvisor类:通过解析正则表达式匹配目标方法。当然它的内部是使用的正则表达式的节点类:JdkRegexpMethodPointcut,开发这直接编写自己的拦截器就好。

使用:

<bean id="settersAndAbsquatulateAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="advice"><ref bean="beanNameOfAopAllianceInterceptor"/></property>// 这里要设置你想拦截的方法的表达式。// 这个表达式会复制给切点类JdkRegexpMethodPointcut<property name="patterns"><list><value>.*set.*</value><value>.*absquatulate</value></list></property>
</bean>

DefaultPointcutAdvisor这个类只是一个切面类,需要自己定义切点和增强。默认的切点是匹配所有的类,如果这样符合预期,那么只需要定义增强类。

增强的拦截

增强的逻辑是放在拦截器中执行的:

public class DebugInterceptor implements MethodInterceptor {// MethodInvocation 是一个拦截器执行链,调用proceed()会执行下一个拦截器。// 所以可以在proceed()前后执行自己的逻辑。public Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("Before: invocation=[" + invocation + "]");Object rval = invocation.proceed();System.out.println("Invocation returned");return rval;}
}

使用ProxyFactoryBean创建代理

一般创建代码的时候,spring通过bean的postBeanProcessor处理器,对bean进行拦截,创建代理。还有一种方法就是通过ProxyFactoryBean创建代理,但是这种方式只能对一个bean进行代理。例子可以看:TransactionProxyFactoryBean

使用spring aop自己创建代理

ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);
factory.addAdvice(myMethodInterceptor);
factory.addAdvisor(myAdvisor);
MyBusinessInterface tb = (MyBusinessInterface) factory.getProxy();

自动代理

之前介绍的都是代理一个指定类。那么spring是如何代理容器的类呢?使用的是beanPostProcessor处理器。在初始化后方法中,找到所有切面类,对该bean进行匹配。如果符合,就代理。

包装代理对象

org.springframework.aop.framework.Advised接口实现了可以对代理对象的所有操作。

Advisor[] getAdvisors();void addAdvice(Advice advice) throws AopConfigException;void addAdvice(int pos, Advice advice) throws AopConfigException;void addAdvisor(Advisor advisor) throws AopConfigException;void addAdvisor(int pos, Advisor advisor) throws AopConfigException;int indexOf(Advisor advisor);boolean removeAdvisor(Advisor advisor) throws AopConfigException;void removeAdvisor(int index) throws AopConfigException;boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;boolean isFrozen();

封装之后可以进行调节:

Advised advised = (Advised) myObject;
Advisor[] advisors = advised.getAdvisors();
int oldAdvisorCount = advisors.length;
System.out.println(oldAdvisorCount + " advisors");// Add an advice like an interceptor without a pointcut
// Will match all proxied methods
// Can use for interceptors, before, after returning or throws advice
advised.addAdvice(new DebugInterceptor());// Add selective advice using a pointcut
advised.addAdvisor(new DefaultPointcutAdvisor(mySpecialPointcut, myAdvice));assertEquals("Added two advisors", oldAdvisorCount + 2, advised.getAdvisors().length);

【Spring-AOP】Spring提供的AOP开发方式和底层AOP开发方式相关推荐

  1. Spring - Java/J2EE Application Framework 应用框架 第 5 章 Spring AOP: Spring之面向方面编程G

    第 5 章 Spring AOP: Spring之面向方面编程 5.1. 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层 ...

  2. Spring - Java/J2EE Application Framework 应用框架 第 5 章 Spring AOP: Spring之面向方面编程

    第 5 章 Spring AOP: Spring之面向方面编程 5.1. 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层 ...

  3. 第 5 章 Spring AOP: Spring之面向方面编程

    http://oss.org.cn/ossdocs/framework/spring/zh-cn/aop.html 第 5 章 Spring AOP: Spring之面向方面编程 5.1. 概念 面向 ...

  4. 【Spring】Spring第二天 - AOP 详解、动态代理设计模式(JDK和cglib)

    一.AOP AOP 和 Filter 能够实现的 功能相似. AOP 和 Filter 的区别: AOP拦截的是类中方法(切点),只要方法能够被Spring管理,那么这个方法就能够被拦截. Filte ...

  5. 学习Spring Boot:(二十二)使用 AOP

    前言 AOP,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.基于AOP实现的功能不会破坏原来程序逻辑,因此它可以很好的对业务逻辑的各个部分进行隔离,从而使得业务逻 ...

  6. 【小家Spring】Spring AOP各个组件概述与总结【Pointcut、Advice、Advisor、Advised、TargetSource、AdvisorChainFactory...】

    每篇一句 基础技术总是枯燥但有价值的.数学.算法.网络.存储等基础技术吃得越透,越容易服务上层的各种衍生技术或产品 相关阅读 [小家Spring]Spring AOP原理使用的基础类打点(AopInf ...

  7. 【Spring】Spring AOP源码分析-导读(一)

    文章目录 1.简介 2.AOP 原理 3.AOP 术语及相应的实现 3.1 连接点 - Joinpoint 3.2 切点 - Pointcut 3.3 通知 - Advice 3.4 切面 - Asp ...

  8. Spring 学习 day3 : AOP,Spring中JdbcTemplate的使用

    1.AOP 1.1 什么是AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 式和运行期动态代理实现程序功能的统一维护的一种技术. ...

  9. java学习day40(Spring)spring中的aop和基于XML以及注解的AOP配置

    第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP AOP :全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程 ...

  10. Spring AOP 源码系列(一)解析 AOP 配置信息

    在进行源码阅读之前建议先看一下这篇文章:Spring AOP 源码分析系列文章导读 by 田小波,写的非常好,推荐阅读. 关于 AOP 中常用的一些术语这里就不解释了,如果不清楚的建议先看一遍上面推荐 ...

最新文章

  1. Eclipse中将项目中build path底下的jar发布到tomcat下
  2. Java基础——组合关系
  3. 开源库的使用方法以及libjpeg的移植详解
  4. python3 exe_Windows10下python3和python2同时安装 python2.exe、python3.exe和pip2、pip3设置
  5. linux下硬盘的安装及分区fdisk
  6. (转)unity web 缓存解决方案
  7. 数据管理的3种方法,看完后感叹:数字化转型、数据中台真不难
  8. SQL SERVER模糊匹配查询
  9. WORD批量更改所有图片大小
  10. android网络编程登录和验证,ASP.NET实现用户注册和验证功能(第4节)
  11. mysql中exists 和 in的用法你还真不知道
  12. Android 热修复框架: Sophix基本使用
  13. wps怎么把ppt里的字体一起保存_WPS怎样将PPT中的文字导出为Word文档?
  14. Mac下载pd虚拟机以及激活
  15. 游戏本自动掉帧_实用 | 大夏天,如何解决卡顿掉帧?
  16. 什么造成了社会普遍的人情冷漠?
  17. Elasticsearch提高查询性能的方法
  18. 什么是报表工具?和 EXCEL 有什么区别?
  19. Hadoop3.X环境配置
  20. 【重点突破】—— UniApp微信小程序开发教程学习Three

热门文章

  1. visio 生成mysql脚本_Visio2010建立ER图并直接导出为SQL语句
  2. 解决PCL 编译报错:undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>:: XXX
  3. 考研南邮和杨大计算机,江苏省这4所“非211”低调有实力,从不争名次,毕业生颇受欢迎...
  4. 阿里云云计算 41 阿里云CDN的工作原理
  5. 翻译:Swift5 使用日期类型:Date、DateFormatter、DateComponent
  6. 算法:回溯和动态规划解决每次移动一步最终回到原地1269. Number of Ways to Stay in the Same Place After Some Steps
  7. 剑指offerJZ1 二维数组中的查找 C++ python
  8. 可访问性之于类和对象
  9. K-Means与KNN比较
  10. Ubuntu 自动安装libsvm