简体   繁体   中英

Spring AOP not working for model class in spring boot

My aspect class is calling the aspect while startup.

but after calling the api mentioned below the model setter methods are not handled through aspect

@RestController
@AllArgsConstructor
@Slf4j
public class MyController {
  @PostMapping("/terms")
  public Mono<ResponseEntity<ModelResponse>> createCountryExceptionRule(@RequestBody ModelRequest model) {
    model.setField1("test");
    //some statements
  }
}
@Data
class ModelRequest{
  String field1;
  String field2;
}
@Aspect
@Component
class MyAspect {
  @Before("execution(public * *.set*(..))")
  public void test(JoinPoint joinPoint) {
    System.out.println("Before method:" + joinPoint.getSignature());
  }
}
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class MyApplication {
  public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
  }
}

dependency i have added is below

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

The aspect is not getting called if I'm calling the api through http request

what's wrong am I doing here or anything I'm missing?

Spring AOP only works on Spring Container managed beans. From the documentation : 5.2. Spring AOP Capabilities and Goals

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans)

The ModelRequest parameter instance of createCountryExceptionRule() method is not a Spring bean ( annotating the ModelRequest class with @Component has no effect here).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM