Customizing Proxy Generation

The default behaviour of the Around annotation is to generate a proxy at compile time that is a subclass of the class being proxied. In other words, in the previous example a compile time subclass of the NotNullExample class will be produced where methods that are proxied 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 are trying to implement you may wish to alter this behaviour and 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 will delegate to the original bean instance

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

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