昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点。太他爷爷的有道理了!要说看人品,还是女孩子强一些。原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子。哥哥们,不想这么远行吗?看看何洁,看看带着俩娃跳楼的妈妈。所以现在的女孩子是很明白的,有些男孩子个子不高,其貌不扬,但是一看那人品气质就知道能找个不错的女盆友。不过要说看人的技术能力,男孩子确实更胜一筹,咱得努力了。

  总结一下要形成的习惯:

  1>有空时隔一段时间要做几道算法题,C语言和JAVA都可以,主要是训练思维。

2>定期阅读spring的源码。因为spring是框架,重设计,能够培养大局观。

  3>阅读底层的书籍,如linux方面,虚拟机方面,这是内功。越高级的语言只是招式。

  4>不要忘记做了一半的东西,如搜索引擎方面,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的核心概念了。总结一下:

方面(Aspect)
连接点(Joinpoint)
通知(Advice)
切入点(Pointcut)
引入(Introduction)
目标对象(Target Object)
AOP代理(AOP Proxy)
织入(Weaving)

  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.
*
* <p>{@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, <em>s</em>, is present
* and any of the following conditions is met:
* <ul>
* <li> 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;</li>
* <li> 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};</li>
* <li> 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.</li>
* </ul>
* @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<Void>() {
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的一些语法,如果不太了解,建议先看看<java8 in action>这本书。代码看着不少,实际上都在进行一些安全校验,包装之类的,真正有用的就两句: 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动态代理的原理都已经很明白了。这里要说一点理论性的东西:

  AOP解决的问题往往可以用代理模式来解决。Java开发中常说动态代理和静态代理,而AOP就是动态代理,因为代理的类是在运行时才生成的。而一般说的代理模式写成的代码是编译期就已经生成的,叫静态代理。

介绍我的家乡

  下面是一如既往的跑题时间:俺是山东人。生在潍坊,长在枣庄。潍坊有一些名胜古迹,文化名人。枣庄也有一些名人,铁道游击队的故乡。额,但是也有些名声不太好。特别是我们那个县,叫“薛城”,是孟尝君被封的薛地,所以……盛产鸡鸣狗盗之徒。传乾隆下江南路过此地,进一家人家讨口水喝,被此人家婆娘打将出来,乾隆怒嗔曰:此地穷山恶水,泼妇刁民。

  

专治不会看源码的毛病--spring源码解析AOP篇的更多相关文章

  1. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  2. IDEA阅读spring源码并调试

    目标:搭建起Spring源码阅读和代码调试跟踪的环境,顺便建立一个简单的Demo,能够调试Spring的源代码 本节,主要介绍一下Spring源码阅读和调试的相关环境搭建,并使用MVN创建一个非常简单 ...

  3. spring源码解析--事务篇(前篇)

    对于每一个JAVA程序员,spring应该是再熟悉不过的框架了,它的功能有多强大我就不多说了,既然他有这么强大的功能,是如何实现的呢?这个就需要从他的原理去了解,而最直接了解原理的方式莫过于源码.当然 ...

  4. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  5. JDK源码及其他框架源码解析随笔地址导航

    置顶一篇文章,主要是整理一下写过的JDK中各个类的源码及其他框架源码解析的文章,方便自己随时阅读也方便网友朋友们阅读与指正 基础篇 从为什么String=String谈到StringBuilder和S ...

  6. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  7. Spring源码深度解析

    Spring源码分析 Spring Web入门及源码学习 [Spring源码分析]Bean加载流程概览 Spring Framework 实现原理与源码解析系统 Spring源码分析--水门 Spri ...

  8. Spring源码学习01:IntelliJ IDEA2019.3编译Spring5.3.x源码

    目录 Spring源码学习01:IntelliJ IDEA2019.3编译Spring5.3.x源码 前言 工欲善其事必先利其器.学习和深读Spring源码一个重要的前提:编译源码到我们的本地环境.这 ...

  9. Spring源码解析 | 第一篇 :IntelliJ IDEA2019.3编译Spring5.3.x源码

    前言 工欲善其事必先利其器.学习和深读Spring源码一个重要的前提:编译源码到我们的本地环境.这样方便我们在本地环境添加注释.断点追踪.查看类或接口的继承关系等等,更加高效的学习Spring源码.个 ...

随机推荐

  1. Echarts笔记——使用AJAX填充数据

    最近把编辑器从Sublime换成HBuilder,感觉好用很多啊,可能自己插件没弄好吧.不不过HBuilder的启动速度确实慢,放机械盘启动要7-13秒,还好有固态. 因为项目需要,这周上手了百度的E ...

  2. Ionic 常用组件解析

    Ionic 常用组件解析 $ionicModal(弹出窗口): //创建一个窗口 //此处注意目录的起始位置为app $ionicModal.fromTemplateUrl('app/security ...

  3. PHP接入阿里云市场 阿里短信服务DEMO

    阿里短信服务:支持三大运营商短信.智能管道等优质能力,产品特点:3秒可达.99%到达率.超低资费. 使用阿里短信服务步骤: 1.购买服务 到https://market.aliyun.com/prod ...

  4. 超好用的memcache管理及可视化监控工具,真方便!

    memcache做为主流的缓存数据库之一,广泛在各互联网平台使用,但是大家使用中都知道memcache目前没有一个比较好用的可视化客户端工具,每次都要输入命令进行操作,十分不方便.  而另一款主流缓存 ...

  5. 我的学习之路_第二十七章_jQuery

    jQueryjs类库 把常用对象或者方法封装起来,让我们写代码效率更高 1.jQuery 2.extjs [jQuery入门] jQuery的引入: 通过script标签的src属性引入 入门: 获取 ...

  6. Java开源博客My-Blog之docker组件化修改

    前言 5月13号上线了自己的个人博客,<Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦>,紧接着也在github上开源了博客的代码,到现在为 ...

  7. 【Stack Overflow -- 原创加工、原创整理、生产实战】-- 深度复制

    一.说明 1.本程序的核心代码不是我原创的,是我在Stack Overflow上搜集后加工出来的,原作者已忘记了~ 2.这段程序是我在上海携程(2014年左右)上班时整理并在生产环境应用的,先后经历了 ...

  8. Python执行系统命令:使用subprocess的Popen函数

    使用subprocess的Popen函数执行系统命令 参考: http://blog.sina.com.cn/s/blog_8f01450601017dlr.html http://blog.csdn ...

  9. Unreal Engine 4 Radiant UI 插件入门教程(二)

    本篇章前提要求:在UE4上安装了Radiant UI插件.如果没有安装,请找其它教程(或者是笔者的其它的教程,目前正在写). 本教程的目的:探讨如何从网页元素中调用蓝图中的内容: 第一步: 写一个网页 ...

  10. MySQL学习笔记(三)

    --回顾 字段类型(列类型):数值型,时间日期型和字符串类型 数值型:整型和小数型(浮点型和定点型) 时间日期型:datetime,date,time,timestamp,year 字符串类型:定长, ...