Java / Micronaut Interview questions
How does Micronaut implement AOP (aspect-oriented programming)?
Micronaut implements AOP the same way it implements DI: at compile time, not through runtime bytecode generation. You define an interceptor by implementing MethodInterceptor and create a custom annotation marked with @Around to trigger it.
@Around @Type(RetryInterceptor.class) @Retention(RUNTIME) public @interface Retryable {} @Singleton public class RetryInterceptor implements MethodInterceptor<Object, Object> { public Object intercept(MethodInvocationContext<Object, Object> ctx) { // retry logic, then ctx.proceed() } }
When you annotate a bean method with @Retryable, the compiler generates a proxy subclass at build time that calls into the interceptor chain and then the original method, with no CGLIB subclassing or JDK dynamic proxy created when the app starts.
This is how built-in features like @Retryable, @CircuitBreaker, @Cacheable, and @Transactional are all implemented, as ordinary compile-time AOP advice rather than special-cased framework internals.
More Related questions...