gpt4 book ai didi

java - AspectJ - 来自外部 JAR 的方面

转载 作者:行者123 更新时间:2023-12-02 10:15:39 25 4
gpt4 key购买 nike

我添加了一个 github 存储库,它准确地显示了我的问题:

https://github.com/runefist/AspectJ-Stackoverflow-Q

<小时/>

简而言之,我有一个项目,我们将其称为 ProjectA。 ProjectA 是一个 microprofile-REST-服务器。另一个项目(我们称之为 ProjectB)是 ProjectA(和其他项目)的依赖项。

  • 项目A(微配置文件-REST-服务器)
  • 项目B(项目A的依赖项)

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();
}
}

我已经测试了该方面,当我在同一个项目中使用它时它可以工作,所以不作为依赖项。

项目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除了像您已经做的那样将其添加为 Maven 依赖项之外,还可以在 AspectJ Maven 配置中添加它:

            <configuration>
<!-- (...) -->
<aspectLibraries>
<aspectLibrary>
<groupId>com.runefist</groupId>
<artifactId>ProjectB</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>

我克隆并测试了你的项目,它是这样工作的。

关于java - AspectJ - 来自外部 JAR 的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com