Spring AOP源码解析——专治你不会看源码的坏毛病!
昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点。太他爷爷的有道理了!要说看人品,还是女孩子强一些。
原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子。哥哥们,不想这么远行吗?看看何洁,看看带着俩娃跳楼的妈妈。
所以现在的女孩子是很明白的,有些男孩子个子不高,其貌不扬,但是一看那人品气质就知道能找个不错的女盆友。不过要说看人的技术能力,男孩子确实更胜一筹,咱得努力了。
总结一下要形成的习惯:
有空时隔一段时间要做几道算法题,C语言和JAVA都可以,主要是训练思维。
定期阅读spring的源码。因为spring是框架,重设计,能够培养大局观
阅读底层的书籍,如linux方面,虚拟机方面,这是内功。越高级的语言只是招式。
不要忘记做了一半的东西,如搜索引擎方面,redis方面,可以过一段时间再做,因为到时候自己的境界有提升,深入程度也会有所增加。
下面是今天的正题。我也很菜,看源码也很费力,所以都会从最容易的入手。先了解其原理,再去看源码。
看源码看熟了,以后再遇到问题,就可以通过源码去了解原理了。
spring的AOP,原理懂了,代码相当简单。这也是为什么我记得我还是个菜鸟的时候,面试人家经常问我这个。
先有个大局观,画张整体的spring结构图。以下是备受吐槽的手绘时间:
如果你觉得我左手字写的实在是不能再难看了的话,我有空可以展示一下右手字
天生做不好的两件事:写不好字,梳不整齐头发。自我感觉最近梳头技术有所改观。
AOP面向切面编程是面向对象的补充。它利用一种横切技术,将一些公共行为封装成叫做“方面”的可重用模块,解耦,增加可维护性。
AOP将系统分为核心关注点和横切关注点两部分。核心关注点就是主业务流程,横切关注点就是上面提到的“方面”。
那么看AOP的源码就是要看横切关注点是怎样和核心关注点整合来发挥作用的。
主业务流程归根到底是一个java方法,而且是对象的方法。在AOP中被称为被通知或被代理对象POJO。
AOP的作用就是将核心关注点和横切关注点组合起来,术语叫做“增强”。最后实际用的是增强后的代理对象。
对核心关注点进行增强就涉及到在哪些地方增强的问题。如方法调用或者异常抛出时做增强这些时机叫做连接点Joinpoint。一个通知将被引发的连接点集合叫做切入点,理解时就可以想正则表达式,通配符来指定多个,而不是单单一个连接点。
在连接点都做了哪些增强呢?增强的内容AOP术语叫“通知”Advice。
Spring里定义了四种Advice:BeforeAdvice,AfterAdvice,ThrowAdvice,DynamicIntroducationAdvice。
许多AOP框架包括spring都是以拦截器作为通知模型。维护一个围绕连接点的拦截器链。其中DynamicIntroducationAdvice是可以引入方法或者字段到核心关注点。
这里有个Introduction,AOP术语叫引入。将增强后的AOP代理组装到系统叫做织入。
上面就是AOP的核心概念了。总结一下:
AOP要做的事情就是:生成代理对象,然后织入。
生成代理对象是经常会被问到的一个问题:Spring提供了两种方式来生成代理对象,JDKProxy和Cglib。
具体使用哪种方式由AopProxyFactory根据AdvisedSupport对象的配置来决定。
默认的策略是如果目标类是接口,则使用JDK动态代理技术,否则使用Cglib来生成代理。
Cglib是基于字节码技术的,使用的是ASM。asm是一个java字节码操纵框架,它能被用来动态生成类或者增强既有类的功能。
ASM可以直接产生二进制class文件,也可以在类被加载入JVM之前动态改变类行为。
下面重点来看看JDK动态代理技术。这是我还是个很菜很菜的菜鸟时为数不多能看懂的源码。因为之前看过Java设计模式,写过类似的例子,所以会比较顺畅。今天先讲这一部分。
下面是调用测试类:
package dynamic.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 实现自己的InvocationHandler
* @author zyb
* @since 2012-8-9
*
*/
public class MyInvocationHandler implements InvocationHandler {
// 目标对象
private Object target;
/**
* 构造方法
* @param target 目标对象
*/
public MyInvocationHandler(Object target) {
super();
this.target = target;
}
/**
* 执行目标对象的方法
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 在目标对象的方法执行之前简单的打印一下
System.out.println("------------------before------------------");
// 执行目标对象的方法
Object result = method.invoke(target, args);
// 在目标对象的方法执行之后简单的打印一下
System.out.println("-------------------after------------------");
return result;
}
/**
* 获取目标对象的代理对象
* @return 代理对象
*/
public Object getProxy() {
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
target.getClass().getInterfaces(), this);
}
}
package dynamic.proxy;
/**
* 目标对象实现的接口,用JDK来生成代理对象一定要实现一个接口
* @author zyb
* @since 2012-8-9
*
*/
public interface UserService {
/**
* 目标方法
*/
public abstract void add();
}
package dynamic.proxy;
/**
* 目标对象
* @author zyb
* @since 2012-8-9
*
*/
public class UserServiceImpl implements UserService {
/* (non-Javadoc)
* @see dynamic.proxy.UserService#add()
*/
public void add() {
System.out.println("--------------------add---------------");
}
}
package dynamic.proxy;
import org.junit.Test;
/**
* 动态代理测试类
* @author zyb
* @since 2012-8-9
*
*/
public class ProxyTest {
@Test
public void testProxy() throws Throwable {
// 实例化目标对象
UserService userService = new UserServiceImpl();
// 实例化InvocationHandler
MyInvocationHandler invocationHandler = new MyInvocationHandler(userService);
// 根据目标对象生成代理对象
UserService proxy = (UserService) invocationHandler.getProxy();
// 调用代理对象的方法
proxy.add();
}
}
执行结果如下:
------------------before---------------
--------------------add---------------
-------------------after-----------------
很简单,核心就是 invocationHandler.getProxy();这个方法调用的Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),target.getClass().getInterfaces(), this); 怎么生成对象的。
/**
* Returns an instance of a proxy class for the specified interfaces
* that dispatches method invocations to the specified invocation
* handler.
*
*
{@code Proxy.newProxyInstance} throws
* {@code IllegalArgumentException} for the same reasons that
* {@code Proxy.getProxyClass} does.
*
* @param loader the class loader to define the proxy class
* @param interfaces the list of interfaces for the proxy class
* to implement
* @param h the invocation handler to dispatch method invocations to
* @return a proxy instance with the specified invocation handler of a
* proxy class that is defined by the specified class loader
* and that implements the specified interfaces
* @throws IllegalArgumentException if any of the restrictions on the
* parameters that may be passed to {@code getProxyClass}
* are violated
* @throws SecurityException if a security manager, s, is present
* and any of the following conditions is met:
*
*
the given {@code loader} is {@code null} and
* the caller's class loader is not {@code null} and the
* invocation of {@link SecurityManager#checkPermission
* s.checkPermission} with
* {@code RuntimePermission("getClassLoader")} permission
* denies access;
*
for each proxy interface, {@code intf},
* the caller's class loader is not the same as or an
* ancestor of the class loader for {@code intf} and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to {@code intf};
*
any of the given proxy interfaces is non-public and the
* caller class is not in the same {@linkplain Package runtime package}
* as the non-public interface and the invocation of
* {@link SecurityManager#checkPermission s.checkPermission} with
* {@code ReflectPermission("newProxyInPackage.{package name}")}
* permission denies access.
*
* @throws NullPointerException if the {@code interfaces} array
* argument or any of its elements are {@code null}, or
* if the invocation handler, {@code h}, is
* {@code null}
*/
@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
Class[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
final Class[] intfs = interfaces.clone();
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
*/
Class cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
final Constructor cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
这个代码是JDK1.8中的,里面用到了1.8的一些语法,如果不太了解,建议先看看这本书。
代码看着不少,实际上都在进行一些安全校验,包装之类的,真正有用的就两句:
Class cl = getProxyClass0(loader, intfs);这句话查找或者生成代理类。跟进去:
/**
* Generate a proxy class. Must call the checkProxyAccess method
* to perform permission checks before calling this.
*/
private static Class getProxyClass0(ClassLoader loader,
Class... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
return proxyClassCache.get(loader, interfaces);
}
对,就是从缓存里把接口拿将出来。然后用return cons.newInstance(new Object[]{h}) 这一句将接口用invocationHandler进行包装。
具体源码可以跟进去看,不详述。想必看到这里,JDK动态代理的原理都已经很明白了。
这里要说一点理论性的东西:郑州 不 孕 不 育 医 院:http://wapyyk.39.net/zz3/zonghe/1d427.html/
AOP解决的问题往往可以用代理模式来解决。Java开发中常说动态代理和静态代理,而AOP就是动态代理,因为代理的类是在运行时才生成的。
而一般说的代理模式写成的代码是编译期就已经生成的,叫静态代理。
Spring AOP源码解析——专治你不会看源码的坏毛病!的更多相关文章
- Mybatis源码解析3——核心类SqlSessionFactory,看完我悟了
这是昨晚的武汉,晚上九点钟拍的,疫情又一次来袭,曾经熙熙攘攘的夜市也变得冷冷清清,但比前几周要好很多了.希望大家都能保护好自己,保护好身边的人,生活不可能像你想象的那么好,但也不会像你想象的那么糟. ...
- Spring AOP实现方式四之注入式AspectJ切面【附源码】
现在我们要讲的是第四种AOP实现之注入式AspectJ切面 通过简单的配置就可以实现AOP了. 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.ao ...
- 【JVM源码解析】模板解释器解释执行Java字节码指令(上)
本文由HeapDump性能社区首席讲师鸠摩(马智)授权整理发布 第17章-x86-64寄存器 不同的CPU都能够解释的机器语言的体系称为指令集架构(ISA,Instruction Set Archit ...
- Spring IOC容器启动流程源码解析(一)——容器概念详解及源码初探
目录 1. 前言 1.1 IOC容器到底是什么 1.2 BeanFactory和ApplicationContext的联系以及区别 1.3 解读IOC容器启动流程的意义 1.4 如何有效的阅读源码 2 ...
- Spring AOP专业术语解析
一. 连接点(Joinpoint) 连接点就是程序执行的某个特定的位置,如:类开始初始化前.类初始化后.类的某个方法调用前.类的某个方法调用后.方法抛出异常后等.Spring 只支持类的方法前.后.抛 ...
- Java源码解析——集合框架(五)——HashMap源码分析
HashMap源码分析 HashMap的底层实现是面试中问到最多的,其原理也更加复杂,涉及的知识也越多,在项目中的使用也最多.因此清晰分析出其底层源码对于深刻理解其实现有重要的意义,jdk1.8之后其 ...
- Java源码解析——集合框架(四)——LinkedListLinkedList原码分析
LinkedList源码分析 LinkedList也和ArrayList一样实现了List接口,但是它执行插入和删除操作时比ArrayList更加高效,因为它是基于链表的.基于链表也决定了它在随机访问 ...
- [Spring] AOP, Aspect实例解析
最近要用到切面来统一处理日志记录,写了个小实例练了练手: 具体实现类: public interface PersonServer { public void save(String name); p ...
- Spring aop(1)--- 寻找切面和代理对象执行流程源码分析
1.基于注解,首先我们是通过@EnableAspectJAutoProxy()这个注解开起AOP功能,这个注解会导入AspectJAutoProxyRegistrar组件从而将AnnotationAw ...
随机推荐
- 利用高德地图javascriptAPI做一个自己的地图
最近由于项目中需要制作一个地图,用来选择活动地点,我就花了两天利用高德地图的javascriptAPI自制了一个地图的demo.在这了记录一下我学习的过程. 一.进入高德地图官网,再找到高德地图的开放 ...
- 03_主线程联网问题&ANR&子线程不能修改UI
如果不使用HAXM,恐怕网页源码查看器无法获取servlet的源码.初步猜测是安卓模拟器运行速度太慢了.如果CPU不支持VT-x的话,HAXM是安装不上的.所以可以先开启VT-x. 可以参考几篇文章h ...
- Harbor安装部署--基于 Docker Distribution 的企业级 Registry 服务
harbor简介 Harbor 是一个企业级 Registry 服务.它对开源的 Docker Registry 服务进行了扩展,添加了更多企业用户需要的功能.Harbor 被设计用于部署一套组织内部 ...
- Codeforces645C【二分】
题意: 给你一个序列,0表示空,1表示非空 你需要填k+1个位置,然后找出某一点到其他所有点都是最近的,然后输出一个最近的情况的最远点. 思路: 哎,好菜哦...不会写这个二分... 遍历每个可取的位 ...
- hdoj5821【贪心-神题】
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊,比赛的时候直接读错题了,实力带坑队友.... 题意: 有两个序列都代表筐,每个筐里只有一个球,然后序列的值代表筐里的球的颜色,问你在m次操作后,a序列的球能否变成b ...
- Linux下的录屏软件Kazam
发现Ubuntu下一个很好用的录屏软件kazam,Ubuntu官方源中就有. 1.安装 $ sudo apt-get install kazam 2.使用 使用很简单,除了截图,还可以录制屏幕视频.既 ...
- NSA互联网公开情报收集指南:迷宫中的秘密·下
猫宁!!! 参考链接: https://www.nsa.gov/news-features/declassified-documents/assets/files/Untangling-the-Web ...
- ubuntu 下配置django 项目能够被局域网下的其他电脑访问
在项目下的路径下下运行 python manage.py runserver 后面的端口换成其他可用的端口也可以 如何让外网也能访问呢,有待更新
- C#程序结构与基本语法
C# 程序结构 Hello World 实例 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declaration) 一个 class Class 方法 Class 属性 一个 ...
- size_t与size_type区别
size() 标准库string里面有个函数size,用来返回字符串中的字符个数,具体用法如下: string st("The expense of spirit\n");cou ...