Ich bin ziemlich neu in AOP. Ich versuche, Anmerkungen in einem Maven-Projekt ohne Spring mit AspectJ zu erstellen. Allerdings wird meine Methode, die ich mit @aspect aufrufen möchte, nicht aufgerufen. DieseVerwenden von AspectJ-Annotationen ohne Spring
ist, was mein pom wie folgt aussieht:
<?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>
Die Anmerkung wie folgt aussieht:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HasAccess {
Access[] accesses();
String message() default "You are not allowed to perform this operation";
}
ich eine Anmerkung Prozessor für meine Anmerkung erstellt:
@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);
}
}
ich es in diesem Aufruf Aufruf:
public class TestMe {
@HasAccess(accesses = {Access.CREATE_PURCHASE})
public void createPurchase(BigDecimal bigDecimal) {
System.out.println("create Purchase called");
}
}
Ich habe eine aop.xml Datei und platziert es in dem gleichen Ordner wie pom.xml.
Wenn ich die Methode createPurchase aufrufen, wird es ausgeführt, ohne dass die @Before -Methode zuerst aufgerufen wird. Bitte hilf mir mit was ich vermisse. Die meisten der Dokumente/Antworten, die ich fand, waren Spring Alignment. Irgendwelche Hinweise auf irgendein Tutorium oder sogar eine andere Art des Erstellens einfacher Annotationen ohne Frühling würden sehr geschätzt.
Wie sind Sie mit Ihrem Code? – mateuszlo