Spring AOP
June 22, 2020

M02 Q04 Which are the limitations of the two proxy-types?

Which are the limitations of the two proxy-types?

Some limitations are similar, for example, neither proxy supports self-invocation. Most of the requirements come from the way how the proxy works.

JDK Dynamic Proxy Limitations:

  • Does not support self-invocation
  • The class must implement the interface
  • The only method implementing the interface will be proxied

CGLIB Proxy Limitations:

  • Does not support self-invocation
  • Class for which proxy should be created can not be final
  • A method which should be proxied cannot be final
  • Only public/protected/package methods will be proxied, private methods are not proxied

What visibility must Spring bean methods have to be proxied using Spring AOP?

Spring Bean Method needs to have the following visibility level to be proxied:

  • JDK Dynamic Proxy public. If you are implementing an interface you cannot make a method that is different than the public because you will lower the visibility access to the method.
  • CGLIB Proxypublic/protected/package

On top of the requirement above, for the call to be proxied, it needs to come from outside, both JDK Dynamic Proxy and CGLIB proxy does not support self-invocation.