Snehasis Rout
Snehasis Rout

Reputation: 69

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?

Upvotes: 1

Views: 836

Answers (1)

R.G
R.G

Reputation: 7121

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

Upvotes: 1

Related Questions