私はAspectjでAOPを実行しようとしていますが、なぜ私のアスペクトを実行していないのか分かりません。私はこれを初めて行うのですから、私は間違ったことをしているかもしれません。AspectJのアドバイスは、Mavenのマルチモジュール設定で起動しません。
これは私のコードです:
アスペクト比:
@Aspect
public class YourAspect {
@Pointcut("@annotation(yourAnnotationVariableName)")
public void annotationPointCutDefinition(YourAnnotation yourAnnotationVariableName){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint, YourAnnotation yourAnnotationVariableName) throws Throwable {
if(yourAnnotationVariableName.isRun()) {
Object returnObject = null;
try {
System.out.println("aspects.YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
} finally {
System.out.println("aspects.YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.");
}
return returnObject;
}
return joinPoint.proceed();
}
@After("annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()")
public void printNewLine(JoinPoint pointcut, YourAnnotation yourAnnotationVariableName){
System.out.print("End\n\r");
}
}
注釈:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface YourAnnotation {
public boolean isRun() default true;
}
メインクラス:
public class MainClass{
public static void main(String[] args) {
MainClass yourClass = new MainClass();
yourClass.yourMethodAround();
}
@YourAnnotation
public void yourMethodAround(){
System.out.println("Executing TestTarget.yourMethodAround()");
}
}
私は2つで働いていますモジュール、POMsこのようなKS:
アスペクトのPOM:
<?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>Group</groupId>
<artifactId>Aspects</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</project>
主なPOM:
<?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>Group</groupId>
<artifactId>Main</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<pluginManagement>
<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>
<aspectLibraries>
<aspectLibrary>
<groupId>Group</groupId>
<artifactId>Aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>aspects.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>a-make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>aspects.MainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>Group</groupId>
<artifactId>Aspects</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
私はMVNクリーンMVNのexecその後、両方のプロジェクトにインストールしました:メインプロジェクトでJava、およびそれだけアスペクトではなくメソッドを実行します。誰でも助けてくれますか?
ありがとうございました!
文字通りあなたのポイントカットで 'yourAnnotationVariableName'を使用していますか?パラメータとして注釈タイプが必要です。https://blog.jayway.com/2015/09/08/defining-pointcuts-by-annotations/ –
私はそのWebページに従いました。私のpointcutでyourAnnotationVariableNameを使うのはどういう意味ですか?あなたはもう少し説明できますか? – Motomine
一般的なMavenマルチモジュールプロジェクトや、特にAspectJ Mavenプラグインの使い方には、いくつかの問題と誤った仮定があります。たとえば、アスペクトモジュールはプラグインのバージョンを構成しませんが、メインモジュールを親として使用しません。たとえアスペクトモジュールがメインとメインに依存している場合は、依存関係としてのアスペクトがあるため、これはコンパイルできないため、循環参照を意味します。私はあなたが共通の親を作成してから、正しい構成のアプリケーションとアスペクトモジュールを作成することをお勧めします(私はあなたにそれを手助けすることができます)。 – kriegaex