13.3.3 Kotlin and AOP Advice

Micronaut provides a compile-time AOP API that does not use reflection. When you use any Micronaut AOP Advice, it creates a subclass at compile-time to provide the AOP behaviour. This can be a problem because Kotlin classes are final by default. If the application was created with the Micronaut CLI, the Kotlin all-open plugin is configured for you to automatically change your classes to open when an AOP annotation is used. To configure it yourself, add the Around class to the list of supported annotations.

If you prefer not to or cannot use the all-open plugin, you must declare the classes that are annotated with an AOP annotation to be open:

  1. import io.micronaut.http.annotation.Controller
  2. import io.micronaut.http.annotation.Get
  3. import io.micronaut.http.HttpStatus
  4. import io.micronaut.validation.Validated
  5. import javax.validation.constraints.NotBlank
  6. @Validated
  7. @Controller("/email")
  8. open class EmailController { (1)
  9. @Get("/send")
  10. fun index(@NotBlank recipient: String, (1)
  11. @NotBlank subject: String): HttpStatus {
  12. return HttpStatus.OK
  13. }
  14. }
1if you use @Validated AOP Advice, you need to use open at class and method level.
The all-open plugin does not handle methods. If you declare an AOP annotation on a method, you must manually declare it as open.