Spring AOP源码解析——专治你不会看源码的坏毛病!

虽然现在大厂内卷现象泛滥,而且996的传统依旧肆虐。但没有哪位程序员能架得住互联网大厂的高薪职位诱惑。特别是我还有一位在阿里工作7年多的老表,在其耳旁风之下,不断将大厂描绘的美丽风景刻画在我脑海中,也让我一直有着想进大厂镀金的梦想。

所以为了完成这次进大厂的梦想,前段时间特意拜托老表爆肝一周之后,才梳理好的这份10W字的“Java高级程序员面试精华题”也帮助我在金三银四的最后时段赶上了跳槽季的末班车,成功入职字节!

虽然金三银四黄金跳槽期虽然已过,如果你现在还想跳槽进入大厂,后面的金九银十也不失为一个好机会。利用这4-5个月的时间里好好储备下技术能力,刷一刷面试题。也为跳槽作一作万全准备。

现在我把这份文档出来给每位看到的有缘人,为大家节省一点找资料、翻文献、刷题的时间。

image

第一,要形成的习惯:

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

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

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

4.不要忘记做了一半的东西,如搜索引擎方面,redis方面,可以过一段时间再做,因为到时候自己的境界有提升,深入程度也会有所增加。

第二,进入正题,了解原理,再看源码。

1.看源码看熟了,以后再遇到问题,就可以通过源码去了解原理了。

2.spring的AOP,原理懂了,代码相当简单。这也是为什么我记得我还是个菜鸟的时候,面试人家经常问我这个。

3.先有个大局观,画张整体的spring结构图。

以下是备受吐槽的手绘时间:

image

(如果你觉得我左手字写的实在是不能再难看了的话,我有空可以展示一下右手字。)

AOP面向切面编程是面向对象的补充。它利用一种横切技术,将一些公共行为封装成叫做“方面”的可重用模块,解耦,增加可维护性。

AOP将系统分为核心关注点和横切关注点两部分。核心关注点就是主业务流程,横切关注点就是上面提到的“方面”。那么看AOP的源码就是要看横切关注点是怎样和核心关注点整合来发挥作用的。

主业务流程归根到底是一个java方法,而且是对象的方法。

在AOP中被称为被通知或被代理对象POJO。AOP的作用就是将核心关注点和横切关注点组合起来,术语叫做“增强”。最后实际用的是增强后的代理对象。

对核心关注点进行增强就涉及到在哪些地方增强的问题。如方法调用或者异常抛出时做增强这些时机叫做连接点Joinpoint。一个通知将被引发的连接点集合叫做切入点,理解时就可以想正则表达式,通配符来指定多个,而不是单单一个连接点。

在连接点都做了哪些增强呢?增强的内容AOP术语叫“通知”Advice。

Spring里定义了四种Advice:BeforeAdvice,AfterAdvice,ThrowAdvice,DynamicIntroducationAdvice。

许多AOP框架包括spring都是以拦截器作为通知模型。维护一个围绕连接点的拦截器链。其中DynamicIntroducationAdvice是可以引入方法或者字段到核心关注点。

这里有个Introduction,AOP术语叫引入。将增强后的AOP代理组装到系统叫做织入。

上面就是AOP的核心概念了。总结一下:

image

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 2022-12-9

*

*/

public interface UserService {

/**

* 目标方法

*/

public abstract void add();

}

package dynamic.proxy;

/**

* 目标对象

* @author zyb

* @since 2022-12-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 2022-12-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动态代理的原理都已经很明白了。

这里要说一点理论性的东西:

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

而一般说的代理模式写成的代码是编译期就已经生成的,叫静态代理。

想继续了解JAVA知识的,请管主页

image

# 最后 【这里想说,因为自己也走了很多弯路过来的,所以才下定决心整理,收集过程虽不易,但想到能帮助到一部分自学java想提升Java架构师技术的,P5-P6-P7-P8 的人,心里也是甜的!有需要的伙伴请点㊦方】↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值