AspectJ-来自外部JAR的方面

时间:2019-02-15 10:41:40

标签: java xml aspectj microprofile

我添加了一个github仓库来准确显示我的问题:

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


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

  • ProjectA(microprofile-REST服务器)
  • 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项目中使用它时,它可以工作,因此不是依赖关系。

ProjectA-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>

ProjectB-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来使其工作?

1 个答案:

答案 0 :(得分:1)

在项目A中,除了像已经那样将其作为Maven依赖项添加之外,还需要在AspectJ Maven配置中将B作为aspect library添加:

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

我克隆并测试了您的项目,它是通过这种方式工作的。