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. Python——数据类型公用功能

    1.索引 表示从哪里开始取值. 支持类型 str 列表 dict 不支持类型 int bool a= '1234567' print(a[1:])#指从哪开始读取,1为从1号元素开始.默认为0 pri ...

  2. 使用protobuf (proto3, C++和go语言)

    在这里,我先讲述C++使用protobuf,之后,会补充使用go语言使用protobuf. 使用protobuf需要有如下步骤: 在.proto文件中定义消息(message)格式. 使用protob ...

  3. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

    前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...

  4. shell脚本——正则表达式

    什么是正则表达式 正则表达式分为基础正则和扩展正则,都是为了匹配符合预期要求的字符串 正则表达式与通配符的区别 只需要记住,对文件内容或是展示文本的操作都是正则,而对目录或文件名的操作则都是通配符(例 ...

  5. ET·ci — 全自动软件测试调度(持续集成)平台

            ET·ci 提供了编译-测试-发布解决方案,包括:自动提取配置库代码进行自动构建, 自动调度静态测试工具(如QAC)进行静态测试,自动调度单元测试工具(如Tessy)开展动态测试,自动 ...

  6. python3 queue队列

    一.queue队列 1.python3中的队列模块是queue,不是Queue 2.一般涉及到同步,多线程之类用到队列模块 3.定义了 queue.Queue 类,以及继承它的 queue.LifoQ ...

  7. postgresql —— 数组类型

    创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...

  8. vue中超简单的方法实现点击一个按钮出现弹框,点击弹框外关闭弹框

    效果图展示: View层 <template> <div> <div class="mask" v-if="showModal" ...

  9. Longest Repeating Subsequence

    Description Given a string, find length of the longest repeating subsequence such that the two subse ...

  10. if语句:求x的绝对值

    #include<stdio.h>void main(){ int x,y; scanf("%d", &x); printf("x=%d\n" ...