美文网首页
java-agent 例子

java-agent 例子

作者: 三云_16d2 | 来源:发表于2018-10-09 13:54 被阅读0次

    记录一下java-agent 例子

    java code:

    package test;
    
    import java.lang.instrument.Instrumentation;
    
    public class TestAgent {
    
        public static void premain(String options, Instrumentation inst) {
            System.out.println("begin do premain");
            init();
        }
    
        public static void init() {
            AppContext.init();
        }
    }
    
    package test;
    
    public class AppContext {
    
        private static String app_id;
    
        public static String getAppId() {
            return app_id;
        }
    
        public static void init() {
            app_id = "my-agent";
        }
    }
    
    

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.demo.biz</groupId>
      <artifactId>my-agent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    
    
      <build>
            <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <archive>
                            <manifestEntries>
                                <Premain-Class>test.TestAgent</Premain-Class>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
    
            </plugins>
        </build>
    
    
    </project>
    

    相关文章

      网友评论

          本文标题:java-agent 例子

          本文链接:https://www.haomeiwen.com/subject/eobeaftx.html