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. thinkphp概述2

    thinkphp概述,thinkphp项目构建流程,thinkphp项目结构,thinkphp配置,thinkphp控制器,thinkphp模型,thinkphp视图,thinkphp的内置模板引擎. ...

  2. 190919 centos系统中python2卸载重装

    问题:某些原因卸载了python2,连带卸载了yum工具. 解决思路: 如果服务器没有什么东西,重装系统最省事.但是如果不允许重装,那就只能按部就班的恢复python2和yum. 步骤: 删除pyth ...

  3. k83 svc

    一,deployment Deployment为Pod和Replica Set下一代Replication Controller)提供声明式更新 1,配置示例 apiVersion: apps/v1 ...

  4. Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)

    题目描述: B. Shaass and Bookshetime limit per test    2 secondsmemory limit per test 256 megabytesinput  ...

  5. .net框架 - File类与FileInfo类异同

    System.IO命名空间中提供的文件操作类有File和FileInfo,这两个类的功能基本相同,只是File是静态类,其中所有方法都是静态的,可以通过类名直接调用,不需要实例化.而FileInfo是 ...

  6. 多线程实现的方式一继承Thread

    实现方法一:继承Thread类 package thread; /** * @function 多线程继承Thread类 * @author hj */ public class Threads ex ...

  7. CloudCompare 的简单的使用说明

    来自:https://blog.csdn.net/datase/article/details/79797795 File open:打开 save:保存 Global Shift settings: ...

  8. mysql模糊查询多个字段

    SELECT * FROM nst_t_conferencehis WHERE ConferName REGEXP '军工|军资';

  9. 洛谷 P1886 滑动窗口 题解

    每日一题 day26 打卡 Analysis 单调队列模板 对于每一个区间,有以下操作: 1.维护队首(就是如果你已经是当前的m个之前那你就可以被删了,head++) 2.在队尾插入(每插入一个就要从 ...

  10. Hive 实现update和delete(转载)

    原文链接:https://blog.csdn.net/xueyao0201/article/details/79387647 因为业务要求,需要对Hive表进行delete,在官网查询后,发现upda ...