目录
  1. 1. spring-instrument源码阅读
    1. 1.1. spring-instrument模块功能
    2. 1.2. asm与instrument
    3. 1.3. spring-instrument代码结构
    4. 1.4. spring-instrument源码
      1. 1.4.1. InstrumentationSavingAgent
    5. 1.5. 文献引用
spring-instrument源码阅读

spring-instrument源码阅读

spring-instrument模块功能

  1. instrument提供了允许java通过代理服务来检测运行在jvm上的程序,可以对方法的字节码进行修改,实现虚拟机级别的aop
  2. instrument功能是在java se5中加入的,instrument要求在运行前用命令行或系统参数来设置代理类.也就是在运行前设置代理类
  3. 在java se6中加入了动态控制的代理的能力
  4. 监控和控制虚拟机的行为。

asm与instrument

  1. asm与instrument一样,都能操作字节码。但是instrument是在整个虚拟机上挂了一个钩子函数,每次装入一个新类,都必须执行这个函数,如果是用在改造类上,会造成很大的资源浪费。instrument更适合与监控和控制虚拟机的行为。
  2. asm和instrument的侧重点不同,asm侧重改造字节码,实现动态代理;instrument侧重监控和控制虚拟机。

spring-instrument代码结构

spring-instrument

spring-instrument源码

InstrumentationSavingAgent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.instrument;

import java.lang.instrument.Instrumentation;

/**
* Java agent that saves the {@link Instrumentation} interface from the JVM
* for later use.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver
*/

/**
* https://www.cnblogs.com/yelao/p/9841810.html
* https://blog.csdn.net/jiangtianjiao/article/details/88093825
* https://www.ibm.com/developerworks/cn/java/j-lo-jpda2/index.html
* instrument提供了允许java通过代理服务来检测运行在jvm上的程序,可以对方法的字节码进行修改,实现虚拟机级别的aop
* instrument功能是在java se5中加入的,instrument要求在运行前用命令行或系统参数来设置代理类.也就是在运行前设置代理类
* 在java se6中加入了动态控制的代理的能力
*
* 下面的实现就是为spring项目提供一个java动态代理的接口
*/
public final class InstrumentationSavingAgent {

private static volatile Instrumentation instrumentation;


private InstrumentationSavingAgent() {
}

/**
* 下面这两个方法都是直接使用的jdk中的instrument
*/

/**
* Save the {@link Instrumentation} interface exposed by the JVM.
* 这个方法是在程序运行前进行代理,也就是在main函数运行之前的一些操作(main执行之前的修改)
*/
public static void premain(String agentArgs, Instrumentation inst) {
instrumentation = inst;
}

/**
* Save the {@link Instrumentation} interface exposed by the JVM.
* 这个方法是在运行时动态代理(控制类运行的行为)
* This method is required to dynamically load this Agent with the Attach API.
*/
public static void agentmain(String agentArgs, Instrumentation inst) {
instrumentation = inst;
}

/**
* Return the {@link Instrumentation} interface exposed by the JVM.
* <p>Note that this agent class will typically not be available in the classpath
* unless the agent is actually specified on JVM startup. If you intend to do
* conditional checking with respect to agent availability, consider using
* {@link org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#getInstrumentation()}
* instead - which will work without the agent class in the classpath as well.
* @return the {@code Instrumentation} instance previously saved when
* the {@link #premain} or {@link #agentmain} methods was called by the JVM;
* will be {@code null} if this class was not used as Java agent when this
* JVM was started or it wasn't installed as agent using the Attach API.
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#getInstrumentation()
*
* 不管是使用运行前代理还是动态代理,都会产生一个instrumentation对象,这个方法就是为spring项目提供这个对象
*/
public static Instrumentation getInstrumentation() {
return instrumentation;
}

}

文献引用

  1. AOP之利器:ASM介绍
  2. 冷门instrument包,功能d炸天
文章作者: rack-leen
文章链接: http://yoursite.com/2020/01/13/%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB/Java/Spring%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB/spring-instrument/spring-instrument%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 rack-leen's blog
打赏
  • 微信
  • 支付宝

评论