记录一下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>
网友评论