[英]How is Load-Time Weaving to a .jar file with AspectJ done?
Some project background: I am working with an application that runs on Java.一些项目背景:我正在使用在 Java 上运行的应用程序。 My goal is to collect real-time data to send it to an API running on a local server.我的目标是收集实时数据以将其发送到在本地服务器上运行的 API。 The application is packaged into a .jar file and I have no access to source code.该应用程序被打包到一个 .jar 文件中,我无法访问源代码。 (Aside from the question, I have attempted to decompile the class to add additional functionality; however, that did not end well, $access000...) (除了这个问题,我试图反编译该类以添加其他功能;但是,结果并不好,$access000 ...)
Having just learned about AOP, I ran to test my options.刚刚了解了 AOP,我跑去测试我的选择。 I spend over a week figuring out Maven Setup and AspectJ with it.我花了一个多星期的时间来弄清楚 Maven Setup 和 AspectJ。
I set up a "helloworld" project in which I have been able to successfully weave aspects (from a .java file) into another .java file.我建立了一个“helloworld”项目,在该项目中,我能够成功地将方面(从 .java 文件)编织到另一个 .java 文件中。 My next step is to create an executable .jar file with the main class and weave the aspect .java class into it - then I would have achieved my desired outcome.我的下一步是使用主类创建一个可执行的 .jar 文件,并将 aspect .java 类编织到其中 - 这样我就会达到我想要的结果。
I am not sure if I am misunderstanding something, I believe that should be something feasable.我不确定我是否误解了某些东西,我相信这应该是可行的。
In AspectJ tools, the aspectpath is where to find binary aspects.在 AspectJ 工具中,aspectpath 是查找二进制方面的地方。 Like the classpath, it can include archives ( .jar and .zip files) and directories containing .class files in a package layout (since binary aspects are in .class files).与类路径一样,它可以在包布局中包含存档( .jar和 .zip 文件)和包含 .class 文件的目录(因为二进制方面位于 .class 文件中)。
Some Reference information:一些参考资料:
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── hellomaven
│ └── quickstart
│ ├── App.java
│ └── AppAspect.java
└── test
├── java
└── resources
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hellomaven</groupId>
<artifactId>quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloMaven</name>
<description>test</description>
<properties>
<java.version>1.8</java.version>
<junit.version>4.5</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.version}</source>
<target>${java.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.version}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
<weaveDependencies>
<weaveDependency>
<groupId>com.hellomaven.quickstart.App</groupId>
<artifactId>hello</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<mainClass>com.hellomaven.quickstart</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.hellomaven.quickstart;
public class App {
public static void main(String[] args) {
hello("World");
}
public static void hello(String world) {
System.out.println("Hello " + world);
}
}
package com.hellomaven.quickstart;
import org.aspectj.lang.annotation.*;
@Aspect
public class AppAspect {
@Before("execution(public static void hello(..))")
public void testAspectBefore() {
System.out.println("Before ok.");
}
}
Wherever and in whichever way you are running your code, you simply need to add -javaagent:/path/to/aspectjweaver.jar
to the Java command line.无论您以何种方式运行代码,您只需将-javaagent:/path/to/aspectjweaver.jar
添加到 Java 命令行。 Furthermore, for LTW you need an aop.xml
in the right place.此外,对于 LTW,您需要在正确的位置放置一个aop.xml
。 For Maven that means:对于 Maven,这意味着:
aop.xml
should be located in src/main/resources
. aop.xml
应该位于src/main/resources
中。-javaagent
parameter to your Surefire plugin (unit tests) or Failsafe plugin (integration tests) configuration.如果您在 JUnit 测试中需要 LTW,则需要将-javaagent
参数添加到 Surefire 插件(单元测试)或 Failsafe 插件(集成测试)配置中。-javaagent
parameter to the plugin configuration there, too.如果您想使用 Exec Maven 插件启动您的编译程序,看起来就是这样,您还需要将-javaagent
参数添加到那里的插件配置中。 Outside of Maven, again you need to add -javaagent
to the command line.在 Maven 之外,您需要再次将-javaagent
添加到命令行。
Sorry for the generic answer, but it is just a reflection of your generic question.对不起,通用答案,但这只是您的通用问题的反映。 If you have a more precise follow-up question, you can comment on this answer.如果您有更精确的后续问题,您可以对此答案发表评论。
Some resources:一些资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.