Aspect Method未被触发



我试图让我的Aspect类工作,但它被完全忽略了。

我有以下文件:

MyAnnotation.java

package annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}

MyAspect.java

package annotations;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("@annotation(annotations.MyAnnotation)*")
public void interceptMethods(final JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}

MyClass.java

package annotations;
import org.springframework.stereotype.Service;
@Service
public class MyClass {
@MyAnnotation
public int myMethod(final int i) {
System.out.println("method: " + i);
return i;
}
}

MyRestController.java

package annotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@Autowired
MyClass myClass;
@GetMapping("/myClass")
private int callMyMethod() {
return myClass.myMethod(1);
}
}

Application.java

package annotations;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>experiments</groupId>
<artifactId>annotations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
</dependencies>
</project>

有人看到问题和如何解决它吗?

我已经尝试了多个Before表达式("执行(* annotation ..(..))"),但我就是看不到它的工作。

I've try Around而不是Before。

我以前试过切入点。

我已经看过文章了:AspectJ @Before注释问题Spring AspectJ,方法执行前的切入点,其中方法或类被注释

我终于做到了。我将把解决方案留给其他需要基本示例的人。

这些是文件:

MyAnnotation.java

package annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}

MyAspect.java -这很棘手,因为:

  1. 注释@Aspect不充分。注释@Component也需要添加到方面类中。如果省略注释@Component,应用程序将忽略该类。
  2. @Before注释中,需要有"和参数(*)"如果省略了参数,将会出现空指针异常。
  3. 代码:

package annotations;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@Before("execution(* annotations.*.*(..)) and args(*)")
public void beforeExecution(final JoinPoint joinPoint) {
System.out.println("5");
}
}

MyClass.java

package annotations;
import org.springframework.stereotype.Service;
@Service
public class MyClass {
@MyAnnotation
public int myMethod(final int i) {
System.out.println("method: " + i);
return i;
}
}

MyRestController

package annotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@Autowired
MyClass myClass;
@GetMapping("/myClass")
private int callMyMethod() {
return myClass.myMethod(1);
}
}

Application.java

package annotations;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>experiments</groupId>
<artifactId>annotations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
</dependencies>

最新更新