1

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?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • `ModelRequest` is not a spring managed bean so AOP cannot work on it. – pleft Oct 01 '21 at 06:51
  • I have annotated it with @Component , still same issue – Snehasis Rout Oct 01 '21 at 10:34
  • I reformatted your sample code, splitting code blocks by class, adding syntax highlighting and unifying indentation. Maybe next time you can do that by yourself. BTW, your classes do not have imports, i.e. unless somebody happens to know the Lombok `@Data` annotation, she is not going to understand where setters are supposed to be called. Please do add some more context to your future questions. Thank you very much. – kriegaex Oct 14 '21 at 07:48

1 Answers1

1

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).

R.G
  • 6,436
  • 3
  • 19
  • 28