我知道Spring避免使用-javaagent vm选项来使它的AspectJ加载时间能够正常工作,而是依靠类加载器来启动代理.
我认为Java规范规定使用Java代理的唯一方法是通过-javaagent vm选项.
我错了吗?有人可以将我定向到可以澄清我的疑问的官方Java规范/文档吗?
解决方法:
我发现了一些有关加载Java代理in this interesting blog post的信息.
Instrumentation Agent To enable JVM instrumentation, you have to
provide an agent (or more) that is deployed as a JAR file. An
attribute in the JAR file manifest specifies the agent class which
will be loaded to start the agent.There is 2 ways to load the agent:
- with a command-line interface: by adding this option to the command-line: -javaagent:jarpath[=options] where jarpath is the path
to the agent JAR file. options is the agent options. This switch may
be used multiple times on the same command-line, thus creating
multiple agents. More than one agent may use the same jarpath.- by dynamic loading: the JVM must implement a mechanism to start agents sometime after the the VM has started. That way, a tool can
“attach” an agent to a running JVM (for instance profilers or ByteMan)After the JVM has initialized, the agent class will be loaded by the
system class loader. If the class loader fails to load the agent, the
JVM will abort.
…
是的,我们将非常欢迎官方文档/规范…
编辑1:最后,我遇到了一些相关的官方文档:用于动态加载代理的API Javadoc,如上面的第二个要点所述:参见here for VirtualMachine class和here for loadAgent method.
编辑2:另请参见this other blog post.它清楚地说明了启动时静态加载Javaagent和运行时动态加载Javaagent之间的区别.