Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AspectJ-来自外部JAR的方面_Java_Xml_Aspectj_Aspectj Maven Plugin_Microprofile - Fatal编程技术网

Java AspectJ-来自外部JAR的方面

Java AspectJ-来自外部JAR的方面,java,xml,aspectj,aspectj-maven-plugin,microprofile,Java,Xml,Aspectj,Aspectj Maven Plugin,Microprofile,我添加了一个github回购协议,它准确地显示了我的问题: 简言之,我有一个项目,我们称之为ProjectA。ProjectA是一个文件REST服务器。另一个项目,我们称之为ProjectB,是ProjectA和其他项目的依赖项 ProjectA文件REST服务器 ProjectA的ProjectB依赖关系 ProjectB包含一个方面: @Aspect public class ElasticSenderAspect { @After("@annotation(elasticsend

我添加了一个github回购协议,它准确地显示了我的问题:

简言之,我有一个项目,我们称之为ProjectA。ProjectA是一个文件REST服务器。另一个项目,我们称之为ProjectB,是ProjectA和其他项目的依赖项

ProjectA文件REST服务器 ProjectA的ProjectB依赖关系 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,当我在同一个项目中使用它时,它可以工作,所以不作为依赖项

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>
    <?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-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>
    <?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添加为Maven依赖项之外,还需要将B添加为AspectJ Maven配置中的A:

com.runefist 项目B
我克隆并测试了您的项目,它是这样工作的。

我可以建议将您的方面移到一些通用的jar maven模块中,并在两个项目中作为依赖项使用它。和Euhm一起,我不明白,将方面移动到另一个模块,并将该模块作为依赖项添加到其他项目,我现在使用它有什么区别?我已经添加了ProjectA的pom,也许会有帮助。好的,我终于明白了,ProjectB实际上是一个方面库。您似乎忘记了将aspectj maven插件添加到项目pom文件中,因此aspectj编译器无法在web服务上工作,并且aspectj编译器没有注入aspect代码,您所拥有的只是一个elasticsender注释。只需将粘贴插件配置代码复制到项目A中,或者将其移动到根pom父模块中,这样整个模块都将具有相同的功能。我已经完成了,但仍然不起作用,我创建了一个github repo,它正好显示了我所说的范围,您可以在这里找到它:。我希望这有助于解决问题,如果您需要任何其他信息,请询问: