java agent 使用

java agent的做用

在字節碼這個層面對類和方法進行修改的技術,可以在不影響編譯的狀況下,修改字節碼。能夠理解spring的aop技術java

如何實現

  1. 實現javaagent須要實現premain方法
  2. 必須在MANIFEST.MF文件中有Premain-Class

demo實現

agent

package com.xueyou.demo.agent;
     
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.CtNewMethod;
     
    import java.lang.instrument.ClassFileTransformer;
    import java.lang.instrument.IllegalClassFormatException;
    import java.security.ProtectionDomain;
     
    public class FirstAgent implements ClassFileTransformer {
        public final String injectedClassName = "com.xueyou.agentdemo.App";
        public final String methodName = "hello";
     
        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            className = className.replace("/", ".");
    //        System.out.println(className);
            if (className.equals(injectedClassName)) {
                CtClass ctclass = null;
                try {
                    ctclass = ClassPool.getDefault().get(className);// 使用全稱,用於取得字節碼類<使用javassist>
                    CtMethod ctmethod = ctclass.getDeclaredMethod(methodName);// 獲得這方法實例
                    ctmethod.insertBefore("System.out.println(11111111);");
                    return ctclass.toBytecode();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

預加載類

package com.xueyou.demo;
 
import com.xueyou.demo.agent.FirstAgent;
 
import java.lang.instrument.Instrumentation;
 
/**
 * Hello world!
 */
public class App {
    public static void premain(String agentOps, Instrumentation inst) {
        System.out.println("=========premain方法執行========");
        System.out.println(agentOps);
        // 添加Transformer
        inst.addTransformer(new FirstAgent());
    }
}

maven指定預加載類

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                       <PremainClass>com.xueyou.demo.App</Premain-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如何使用

這裏有兩種方式指定javaagent:
1)使用命令行:java -javaagent:XXX.jar ddd.jar
2)使用idea的vm option選項。spring

clipboard.png

轉: https://blog.csdn.net/wild46c...apache

相關文章
相關標籤/搜索