AspectJ - 来自外部 JAR 的方面
AspectJ - Aspect from external JAR
我添加了一个 github 回购协议,它准确地显示了我的问题:
https://github.com/runefist/AspectJ-Whosebug-Q
简而言之,我有一个项目,我们称之为 ProjectA。 ProjectA 是一个 microprofile-REST-server。另一个项目,我们称之为 ProjectB,是 ProjectA(和其他项目)的依赖项。
- ProjectA(microprofile-REST-server)
- ProjectB(ProjectA 的依赖项)
ProjectB 包含一个方面:
@Aspect
public class ElasticSenderAspect {
@After("@annotation(elasticsender)") // && execution(* *(..))
public void after(JoinPoint joinPoint, ElasticSender elasticsender) {
WebsiteBehaviour websiteBehaviour = new WebsiteBehaviour();
websiteBehaviour.setBehaviourFunc(elasticsender.behaviourFunction());
websiteBehaviour.setBehaviourType(elasticsender.behaviourType());
ElasticWebsiteBehaviour.sendWebsiteBehaviour(websiteBehaviour);
}
}
在 ProjectA 中我有一个函数:
@GET
@Operation(description = "Authenticate")
@ElasticSender(behaviourFunction = "Authenticate")
public Response authenticate(@HeaderParam("authorization") String authString) {
if (authString == null) {
return StandardResponseMessages.GENERAL_NO_AUTHENTICATION.getResponse();
}
WebAccount webAccount = webAccountService.find(getUsernameFromAuth(authString));
boolean correct = false;
//TODO: CHECK IF ACCOUNT IS VERIFIED
if (webAccount != null) {
if (webAccount.getUsername() != null && webAccount.getPassword() != null) {
if (AuthenticatorUtility.basicAuthenticate(webAccount.getUsername(), webAccount.getPassword(), authString)) {
correct = true;
}
}
}
if (correct) {
return Response.ok(generateTokenString(webAccount)).build();
} else {
return StandardResponseMessages.GENERAL_WRONG_AUTHENTICATION.getResponse();
}
}
我已经测试了 Aspect,当我在 SAME 项目中使用它时它可以工作,所以不是依赖项。
项目 A - pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.runefist</groupId>
<artifactId>FeestjesDoen-Server</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>FeestjesDoen-Server</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<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>
<repositories>
<repository>
<id>BendingHeroes-repo</id>
<url>xxx</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- MAIN DEPENDENCY -->
<dependency>
<groupId>com.runefist</groupId>
<artifactId>WebRest-Utilities</artifactId>
<version>1.0.0</version>
</dependency>
<!-- MICROPROFILE SWAGGER UI -->
<dependency>
<groupId>org.microprofile-ext.openapi-ext</groupId>
<artifactId>swagger-ui</artifactId>
<version>1.0.1</version>
<scope>runtime</scope>
</dependency>
<!-- KAFKA NEEDED -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showWeaveInfo>true</showWeaveInfo>
<encoding>UTF-8 </encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目 B - pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.runefist</groupId>
<artifactId>WebRest-Utilities</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<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>
<!-- MICROPROFILE REPLACES JAVAEE -->
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>2.1</version>
<type>pom</type>
</dependency>
<!-- FOR CREATING JWT TOKENS -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
<type>jar</type>
</dependency>
<!-- FOR THE USE OF HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.1.Final</version>
</dependency>
<!-- MYSQL CONNECTOR FOR HIBERNATE -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
<scope>compile</scope>
</dependency>
<!-- FOR JSON CONVERSION -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<!-- ElasticSearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.5.4</version>
<type>jar</type>
</dependency>
<!-- AspectJ | to add behaviour to methods -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- Junit - TEMP added to test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<!--use this goal to weave all your main classes-->
<goal>compile</goal>
<!--use this goal to weave all your test classes-->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
问题:
在 ProjectB 中测试时出现方面,在 ProjectA 中测试时不会出现方面。我需要向 ProjectA pom 添加什么才能使其正常工作,或者我需要将什么更改为 ProjectB pom 才能使其正常工作?
在项目 A 中,您需要将 B 作为 aspect library 添加到 AspectJ Maven 配置中,此外还要像您已经做的那样将其添加为 Maven 依赖项:
<configuration>
<!-- (...) -->
<aspectLibraries>
<aspectLibrary>
<groupId>com.runefist</groupId>
<artifactId>ProjectB</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
我克隆并测试了你的项目,它是这样工作的。
我遇到了同样的问题,将 spring-aop 依赖项添加到 projectB 帮助了我。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.18</version>
</dependency>
并且projectA中不需要添加aspectj插件,
<dependency>
<groupId>com.runefist</groupId>
<artifactId>ProjectB</artifactId>
<version>1.0.0</version>
</dependency>
我添加了一个 github 回购协议,它准确地显示了我的问题:
https://github.com/runefist/AspectJ-Whosebug-Q
简而言之,我有一个项目,我们称之为 ProjectA。 ProjectA 是一个 microprofile-REST-server。另一个项目,我们称之为 ProjectB,是 ProjectA(和其他项目)的依赖项。
- ProjectA(microprofile-REST-server)
- ProjectB(ProjectA 的依赖项)
ProjectB 包含一个方面:
@Aspect
public class ElasticSenderAspect {
@After("@annotation(elasticsender)") // && execution(* *(..))
public void after(JoinPoint joinPoint, ElasticSender elasticsender) {
WebsiteBehaviour websiteBehaviour = new WebsiteBehaviour();
websiteBehaviour.setBehaviourFunc(elasticsender.behaviourFunction());
websiteBehaviour.setBehaviourType(elasticsender.behaviourType());
ElasticWebsiteBehaviour.sendWebsiteBehaviour(websiteBehaviour);
}
}
在 ProjectA 中我有一个函数:
@GET
@Operation(description = "Authenticate")
@ElasticSender(behaviourFunction = "Authenticate")
public Response authenticate(@HeaderParam("authorization") String authString) {
if (authString == null) {
return StandardResponseMessages.GENERAL_NO_AUTHENTICATION.getResponse();
}
WebAccount webAccount = webAccountService.find(getUsernameFromAuth(authString));
boolean correct = false;
//TODO: CHECK IF ACCOUNT IS VERIFIED
if (webAccount != null) {
if (webAccount.getUsername() != null && webAccount.getPassword() != null) {
if (AuthenticatorUtility.basicAuthenticate(webAccount.getUsername(), webAccount.getPassword(), authString)) {
correct = true;
}
}
}
if (correct) {
return Response.ok(generateTokenString(webAccount)).build();
} else {
return StandardResponseMessages.GENERAL_WRONG_AUTHENTICATION.getResponse();
}
}
我已经测试了 Aspect,当我在 SAME 项目中使用它时它可以工作,所以不是依赖项。
项目 A - pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.runefist</groupId>
<artifactId>FeestjesDoen-Server</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>FeestjesDoen-Server</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<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>
<repositories>
<repository>
<id>BendingHeroes-repo</id>
<url>xxx</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- MAIN DEPENDENCY -->
<dependency>
<groupId>com.runefist</groupId>
<artifactId>WebRest-Utilities</artifactId>
<version>1.0.0</version>
</dependency>
<!-- MICROPROFILE SWAGGER UI -->
<dependency>
<groupId>org.microprofile-ext.openapi-ext</groupId>
<artifactId>swagger-ui</artifactId>
<version>1.0.1</version>
<scope>runtime</scope>
</dependency>
<!-- KAFKA NEEDED -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showWeaveInfo>true</showWeaveInfo>
<encoding>UTF-8 </encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目 B - pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.runefist</groupId>
<artifactId>WebRest-Utilities</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<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>
<!-- MICROPROFILE REPLACES JAVAEE -->
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>2.1</version>
<type>pom</type>
</dependency>
<!-- FOR CREATING JWT TOKENS -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
<type>jar</type>
</dependency>
<!-- FOR THE USE OF HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.1.Final</version>
</dependency>
<!-- MYSQL CONNECTOR FOR HIBERNATE -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
<scope>compile</scope>
</dependency>
<!-- FOR JSON CONVERSION -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<!-- ElasticSearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.5.4</version>
<type>jar</type>
</dependency>
<!-- AspectJ | to add behaviour to methods -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- Junit - TEMP added to test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<!--use this goal to weave all your main classes-->
<goal>compile</goal>
<!--use this goal to weave all your test classes-->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
问题:
在 ProjectB 中测试时出现方面,在 ProjectA 中测试时不会出现方面。我需要向 ProjectA pom 添加什么才能使其正常工作,或者我需要将什么更改为 ProjectB pom 才能使其正常工作?
在项目 A 中,您需要将 B 作为 aspect library 添加到 AspectJ Maven 配置中,此外还要像您已经做的那样将其添加为 Maven 依赖项:
<configuration>
<!-- (...) -->
<aspectLibraries>
<aspectLibrary>
<groupId>com.runefist</groupId>
<artifactId>ProjectB</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
我克隆并测试了你的项目,它是这样工作的。
我遇到了同样的问题,将 spring-aop 依赖项添加到 projectB 帮助了我。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.18</version>
</dependency>
并且projectA中不需要添加aspectj插件,
<dependency>
<groupId>com.runefist</groupId>
<artifactId>ProjectB</artifactId>
<version>1.0.0</version>
</dependency>