没有 -javaagent 参数,AspectJ 不会运行
AspectJ does not runs without -javaagent argument
我正在尝试在一个简单的 java 应用程序中使用 AspectJ,而不使用 Spring。
项目由maven控制。
这里是项目代码,你也可以在GitHub
上看到
App.java
package ge.jibo.aspectj;
public class App {
public static void main(String[] args) {
App app = new App();
app.print("Message from App object...");
}
public void print(String value) {
System.out.println(value);
}
}
LoggerAspect.java
package ge.jibo.aspectj;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LoggerAspect {
@Around("execution(!static * *(..))")
public Object dontLogDuplicates(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println("Message from LoggerAspect object");
return thisJoinPoint.proceed();
}
}
aop.xml 在 resources\META-INF
目录中
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="ge.jibo.aspectj.LoggerAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="ge.jibo.aspectj..*"/>
</weaver>
</aspectj>
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>ge.jibo.aspectj</groupId>
<artifactId>aspectj-demo</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>ge.jibo.aspectj.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
在生成 mvn install
JAR
文件后,我尝试使用 java -jar aspectj-demo.jar
命令执行 JAR
,但只执行 App 对象,LoggerAspect 执行不打印任何东西。
如果我将 -javaagent
参数和 aspectjweaver-1.9.6.jar
传递给 JVM,则执行 Aspect 对象。
- 如何避免每次都传递这个
-javaagent
参数?
- 要不要在
pom.xml
里面加个插件直接执行aspect?
一个简单的solution/workaround是为编译时编织添加aspectj-maven-plugin
(在这种情况下不再需要aop.xml
文件)
并使用 maven-assembly-plugin
将项目及其依赖项打包到可执行 jar 中(还有其他方法可以使用 libs/dependencies 打包可执行 jar)
pom.xml
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<verbose>true</verbose>
<complianceLevel>1.8</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
ge.jibo.aspectj.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行 mvn install
生成额外的 jar 文件 aspectj-demo-jar-with-dependencies.jar
java -jar aspectj-demo-jar-with-dependencies.jar
我正在尝试在一个简单的 java 应用程序中使用 AspectJ,而不使用 Spring。 项目由maven控制。
这里是项目代码,你也可以在GitHub
上看到App.java
package ge.jibo.aspectj;
public class App {
public static void main(String[] args) {
App app = new App();
app.print("Message from App object...");
}
public void print(String value) {
System.out.println(value);
}
}
LoggerAspect.java
package ge.jibo.aspectj;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LoggerAspect {
@Around("execution(!static * *(..))")
public Object dontLogDuplicates(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println("Message from LoggerAspect object");
return thisJoinPoint.proceed();
}
}
aop.xml 在 resources\META-INF
目录中
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="ge.jibo.aspectj.LoggerAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="ge.jibo.aspectj..*"/>
</weaver>
</aspectj>
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>ge.jibo.aspectj</groupId>
<artifactId>aspectj-demo</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>ge.jibo.aspectj.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
在生成 mvn install
JAR
文件后,我尝试使用 java -jar aspectj-demo.jar
命令执行 JAR
,但只执行 App 对象,LoggerAspect 执行不打印任何东西。
如果我将 -javaagent
参数和 aspectjweaver-1.9.6.jar
传递给 JVM,则执行 Aspect 对象。
- 如何避免每次都传递这个
-javaagent
参数? - 要不要在
pom.xml
里面加个插件直接执行aspect?
一个简单的solution/workaround是为编译时编织添加aspectj-maven-plugin
(在这种情况下不再需要aop.xml
文件)
并使用 maven-assembly-plugin
将项目及其依赖项打包到可执行 jar 中(还有其他方法可以使用 libs/dependencies 打包可执行 jar)
pom.xml
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<verbose>true</verbose>
<complianceLevel>1.8</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
ge.jibo.aspectj.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行 mvn install
生成额外的 jar 文件 aspectj-demo-jar-with-dependencies.jar
java -jar aspectj-demo-jar-with-dependencies.jar