Customizing Proxy Generation

The default behaviour of the Around annotation is to generate a proxy at compile time that is a subclass of the proxied class. In other words, in the previous example a compile-time subclass of the NotNullExample class will be produced where proxied methods are decorated with interceptor handling, and the original behaviour is invoked via a call to super.

This behaviour is more efficient as only one instance of the bean is required, however depending on the use case you may wish to alter this behaviour. The @Around annotation supports various attributes that allow you to alter this behaviour, including:

  • proxyTarget (defaults to false) - If set to true, instead of a subclass that calls super, the proxy delegates to the original bean instance

  • hotswap (defaults to false) - Same as proxyTarget=true, but in addition the proxy implements HotSwappableInterceptedProxy which wraps each method call in a ReentrantReadWriteLock and allows swapping the target instance at runtime.

  • lazy (defaults to false) - By default Micronaut eagerly initializes the proxy target when the proxy is created. If set to true the proxy target is instead resolved lazily for each method call.