AOPはかなり新しいです。 AspectJを使用してSpringを使用しないでmavenプロジェクトに注釈を作成しようとしています。しかし、私が@Aspectを使って呼び出す方法は呼び出されていません。Springを使用しないAspectJアノテーションの使用
これは以下のように私のポンポンが見えるものです:注釈は、次のようになります
<?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>test</groupId>
<artifactId>tanvi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HasAccess {
Access[] accesses();
String message() default "You are not allowed to perform this operation";
}
私は私の注釈のための注釈プロセッサを作成:
@Aspect
public class HasAccessAdvice {
// @Before("execution(* *.*(..)) && @annotation(testAnnotation) ")
@Before("execution(* *.*(..)) && @annotation(hasAccess)")
public void myBeforeLogger(JoinPoint joinPoint, HasAccess hasAccess) {
System.out.println("Okay - we're in the before handler...");
System.out.println("The test annotation value is: " + hasAccess.accesses().toString());
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String stuff = signature.toString();
String arguments = Arrays.toString(joinPoint.getArgs());
System.out.println("Write something in the log... We are just about to call method: "
+ methodName + " with arguments " + arguments + "\nand the full toString: "
+ stuff);
}
}
私はこの呼び出しでそれを呼び出しています:
public class TestMe {
@HasAccess(accesses = {Access.CREATE_PURCHASE})
public void createPurchase(BigDecimal bigDecimal) {
System.out.println("create Purchase called");
}
}
私はaop.xmlファイルを作成してのpom.xmlと同じフォルダに置きました。
<aspectj>
<aspects>
<aspect name="HasAccessAdvice"/>
</aspects>
</aspectj>
メソッドcreatePurchaseを呼び出すと、まず@Beforeメソッドが呼び出されることなく実行されます。私が逃しているもので私を助けてください。私が見つけたドキュメンテーション/回答のほとんどは、春に整列されていました。任意のチュートリアルへのポインタや、Springを使わない単純な注釈を作成する別の方法も、非常に感謝しています。
:あなたはJUnitテストを作成している場合
、次のようなロード時間ウィーバーを含めるように
pom.xml
の<build><plugins>
-sectionをsrc/test/resources
にMETA-INF/aop.xml
を入れて調整することができますか? – mateuszlo