Spring IOC设计到的设计模式: 工厂模式,模板方法模式,单例模式

Spring AOP涉及到的设计模式: 工厂模式,代理模式

1、Spring AOP目标

将分散在程序各处的横切关注点剥离出来,并以集中的方式进行表达

使得开发人员专注于业务逻辑的实现而非繁杂的非功能代码,简化了程序编写与单元测试

应用场景:

  日志

  安全

  事务

2、AOP核心概念

Advice(通知)

  定义在连接点处的行为,围绕方法调用而进行注入

Pointcut(切点)

  确定在哪些连接点处应用通知

Advisor(通知器)

  组合Advice和Pointcut

3、Spirng AOP实现

ProxyFactoryBean

  --FactoryBean implementation that builds an  AOP proxy based on beans in Spring BeanFactory.

  --Spring AOP的底层实现与源头

4、ProxyFactoryBean的典型配置

5、ProxyFactoryBean的构成

target

  目标对象,需要对其进行切面增强

proxyInterfaces

  代理对象所实现的接口

interceptorNames

  通知器(Advisor)列表,通知器中包含了通知(Advice)与切入点(Pointcut)

6、ProxyFactoryBean的作用

  总的来说,ProxyFactoryBean的作用可用下面这句话概括

  针对目标对象来创建代理对象,将对目标对象方法的调用转到对相应代理对象方法的调用,并且可以在代理对象方法调用前后执行与之匹配的各个通知器定义好的方法。

7、目标代理对象的创建

Spring通过两种方式来创建目标对象

  JDK动态代理

  CGLIB

8、JDK动态代理

如果目标对象实现了接口,那么Spring就会通过JDK动态代理为目标对象生成代理对象

JdkDynamicAopProxy中getProxy方法

public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
}
Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}

  

9、DefaultAopProxyFactory

	public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface()) {
return new JdkDynamicAopProxy(config);
}
if (!cglibAvailable) {
throw new AopConfigException(
"Cannot proxy target class because CGLIB2 is not available. " +
"Add CGLIB to the class path or specify proxy interfaces.");
}
return CglibProxyFactory.createCglibProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}

  

10、CGLIB代理(Cglib2AopProxy.java)

如果目标类并未实现接口,那么spring就好使用CGLIB库创建代理

创建代理对象

11、Spring AOP拦截(动态代理方式)

实际上是通过InvocationHandler的invoke方法实现的

JdkDynamicAopProxy类本身实现了InvocationHandler接口

JdkDynamicAopProxy类中的invoke方法中

List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

获取配置的拦截器链

在该方式下,拦截器链中各个拦截器的调用时通过ReflectiveMethodInvocation对象中的proceed方法实现的

proceed方法是一个递归方法

	public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
} Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}

  

Spring AOP设计的更多相关文章

  1. spring Aop设计原理

    转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...

  2. Spring AOP: Spring之面向方面编程

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

  3. Spring Aop 应用实例与设计浅析

    0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...

  4. 《Spring设计思想》AOP设计基本原理

    0.前言 Spring 提供了AOP(Aspect Oriented Programming) 的支持, 那么,什么是AOP呢?本文将通过一个另外一个角度来诠释AOP的概念,帮助你更好地理解和使用Sp ...

  5. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  6. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  7. Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  8. 从零开始学 Java - Spring AOP 实现主从读写分离

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  9. [转]彻底征服 Spring AOP 之 实战篇

    Spring AOP 实战 看了上面这么多的理论知识, 不知道大家有没有觉得枯燥哈. 不过不要急, 俗话说理论是实践的基础, 对 Spring AOP 有了基本的理论认识后, 我们来看一下下面几个具体 ...

随机推荐

  1. Nacos Docker集群部署

    参考文档:https://nacos.io/zh-cn/docs/quick-start-docker.html 1.从git上下载nacos-docker项目,本地目录为/docksoft/naco ...

  2. 【JUC】7.CountDownLatch

    Latch:门闩.一种线程通信的方式:当程序不涉及同步,仅仅需要线程通信的时候,使用synchronize或者lock的线程通信等待唤醒机制,就显得太重了: 这时候,可以考虑使用信号量类:CountD ...

  3. 修改bash终端命令行颜色

    要修改linux终端命令行颜色,我们需要用到PS1,PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置.在终端输入命令:#set,即可在输出中找到关于PS1的定义如下: PS1='[ ...

  4. 一些SQL保存

    因为两种Oracle数据库中对于结果行号的处理异同造成的. 在MSSQLServer中,可以使用top和ROW_NUMBER()方法的结合来实现,一般写法比较简单,而Oracle则有很大的不同. 只能 ...

  5. java基础(10)---stream流

    一.stream的应用场景 for遍历的冗余场景:  stream的写法: 二.获取Stream流的常用方式 三.Stream的map映射方法 更简单的写法: 四.Stream的filter过滤方法 ...

  6. :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式

    三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...

  7. 29.LINQ初探

    本页内容: 1.LINQ查询语言的组成部分: 2.使用LINQ方法语法: 3.排序查询结果 orderby 字句: 4.聚合运算符: 5.查询复杂对象: 6.投影:在查询中创建新对象: 7.Disti ...

  8. GT源码阅读

    昨天读了一点GT的代码,做个笔记. 参考阅读顺序:https://gt.qq.com/docs/a/UseGtWithBroadcast.txt 在上面的doc上面找到了对应的板块的代码. 1.采集本 ...

  9. c语言实现整数转换为字符串——不考虑负数

    #include <stdio.h> #include <string.h> #define MAX_LEN 16 #define ESP 1e-5 typedef int i ...

  10. bzoj 2480——扩展BSGS

    题意 给定 $a,b$ 和模数 $p$,求整数 $x$ 满足 $a^x \equiv  b(mod \ p)$,不保证 $a,p$ 互质. (好像是权限题,可见洛谷P4195 分析 之前讲过,可以通过 ...