外部库加载时间编织成库 [英] External library load time weaving as library
问题描述
我有一个带主类的罐子.
我用命令 java -jar my.jar
执行它这个主 jar 依赖于 another.jar
(例如 joda-time.jar
).
I have a jar with a main class.
I execute it with command java -jar my.jar
This main jar depends on another.jar
(e.g. joda-time.jar
).
现在我要截取一个another.jar
的方法,说我要打印日志.
顺便说一句,我想像往常一样使用 my.jar
,我的意思是我会像往常一样调用它:java -jar my.jar
.
Now I want to intercept a method of another.jar
and say I want to print log.
By the way, I want to use my.jar
as usual, I mean I'll call it as always: java -jar my.jar
.
我在 github 上找到了一个非常好的例子关于外部图书馆编织.
这个例子,拦截了joda时间库的一个方法.
它具有 toString()
joda time 方法的方面.
但是,它使用单元测试来拦截和演示.
I have found a very nice example on github about external library weaving.
This example, intercepts a method of joda time library.
It has an aspect for toString()
method of joda time.
However, it uses a unit test to intercept and demonstrate.
我已将给定的示例编译并打包为 my_aspect.jar
.
之后,我将my_aspect.jar
移动到了执行目录下,其中包含my.jar
、joda-time.jar
.
最后,我还将 aspectjrt.jar
和 aspectjweaver.jar
添加到同一目录中.
I've compiled and packed the given example as my_aspect.jar
.
After that, I've moved my_aspect.jar
to the execution directory, which contains my.jar
, joda-time.jar
.
Finally, I've also added aspectjrt.jar
, and aspectjweaver.jar
to the same directory.
当我调用 java -jar my.jar
时,不会发生拦截.
我想我必须告诉 my_aspect.jar
一些要拦截的东西,但我不知道该说什么.
When I call java -jar my.jar
, intercepiton doesn't happen.
I think I have to tell the my_aspect.jar
something to intercept but I don't know what to say.
下面是my.jar的主类.
它只是调用被拦截的方法.
Below is the main class of my.jar.
It simply makes a call to intercepted method.
package com.github.example;
import org.joda.time.DateTime;
public class Main {
public static void main(String[] args) {
System.out.println(new DateTime().toString());
}
}
推荐答案
aspectjrt.jar
和 my_aspect.jar
都需要在类路径上,如果你使用 compile时间编织或二元编织.
Both aspectjrt.jar
and my_aspect.jar
need to be on the class path if you use compile time weaving or binary weaving.
对于加载时编织,您需要使用 java -javaagent:/path/to/aspectjweaver.jar -cp my_aspect.jar -jar my.jar
.您用作模板的项目也这样做,请参阅 此处.
For load-time weaving you need to use java -javaagent:/path/to/aspectjweaver.jar -cp my_aspect.jar -jar my.jar
. The project you use as a template also does it like that, see here.
如果您不想使用 Java 代理,也不希望在 Java 命令行上有任何 AspectJ 引用,那么您唯一的选择是对 another.jar
进行二进制编织,创建一个新版本,然后创建一个超级 JAR(胖 JAR),使用 One-JAR 之类的工具(还有一个 Maven 插件)压缩整个应用程序,包括依赖项和 AspectJ 运行时.
If you don't want to use the Java agent and also don't want any AspectJ reference on the Java command line, then your only option is binary weaving for another.jar
, creating a new version of it, and then creating an uber JAR (fat JAR), zipping up your whole application including dependencies and AspectJ runtime using a tool like One-JAR (there is also a Maven plugin for it).
你的问题缺乏细节,所以我无法更准确地回答.
Your question is lacking detail, so I cannot answer more precisely.
更新:因为你说你更喜欢 OneJar 解决方案,你可以搜索我的相关帖子.不过,那些帖子 org.dstovall
中的组 ID 已经过时了.那个版本很久以前就没有维护了,所以我切换到 com.jolira
fork,使用它,例如对于支持 AspectJ 的应用程序,当然要与 AspectJ Maven 插件结合使用:
Update: Because you said you prefer the OneJar solution, you can search for my related posts. The group ID in those posts org.dstovall
is outdated, though. That version was out of maintenance long ago, so I switched to the com.jolira
fork, using it e.g. like this for AspectJ-enabled applications, of course in combination with AspectJ Maven plugin:
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<onejarVersion>0.96</onejarVersion>
<mainClass>de.scrum_master.app.FooBar</mainClass>
<attachToBuild>true</attachToBuild>
</configuration>
</plugin>
这篇关于外部库加载时间编织成库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!