Scan the QR code at the end of the article or search the wechat public account Xiao Li Budu, you can follow the wechat public account, get more Java related content.

1. Learn with questions

The JDK dynamic proxy mechanism, the disadvantages of the JDK dynamic proxy:

  • Question 1: JDK dynamic proxies are implemented based on interfaces. If a class does not implement an interface, how do you proxy it?
  • Question 2: How to solve the problem that the target class of the JDK dynamic proxy calls another method of its own internally, without passing through the proxy object?

Take a look at Spring AOP’s other proxy mechanism, cglib dynamic proxies.

2. CGLIB dynamic proxy

  • Introduction, principle and application of CGLIB
  • CGLib: a MethodInterceptor
  • CGLib series proxy part 2: CallbackFilter callback
  • CGLib series proxy part 3: Lazy loading LazyLoader
  • CGLib series proxy part (4) : improved version of the delayed loading Dispatcher
  • CGLib series proxy part 5: Interface generator InterfaceMaker
  • Spring Aop Cglib implementation principle in detail
  • AOP tools: INTRODUCTION to ASM 3.0
  • After learning Aop, even the principle of dynamic proxy do not understand?

I find time to sort out the content, the above articles are quite old, take a look to understand how to use it. Let me summarize the main points:

  • JDK dynamic Proxy generation Proxy classes inherit from Proxy and implement Proxy interfaces by calling methods of the parent class through reflection.

  • The CGLIB dynamic proxy generates a subclass of the proxy class, implements the Factory interface, underlies bytecode enhancement through ASM, and then generates a new subclass. Methods are called directly, without reflection.

  • The default implementation of the Spring Boot 2.0 start proxy class is cglib. If you want to use JDK dynamic proxies, you can use Spring.aop.proxy-target-class=false.

  • The target class of the CGLIB proxy calls another method of its own, which also passes through the proxy object, because the underlying class is not called in the reflective way of JDK dynamic proxies, but in the new way of dynamically subclassing.

  • A CGLIB proxy will not be proxied if its target class method satisfies any of the following conditions:

    • Use the final modifier method
    • Use methods of the private type
    • Use package access rights
  • CGLIB proxy methods that want to perform special processing on the return value of the method can use CallbackFilter.

3. Summary

Enhancing target methods through dynamic proxies is also the rationale for implementing Spring AOP. There are two types of dynamic proxy: JDK dynamic proxy and CGLIB dynamic proxy. I’ve already talked about the principles of these two proxy methods, so I hope you can try them out and follow the Spring code to see the underlying principles.

One final brainstorming session: Why does Spring Boot use CGLIB dynamic proxies by default?

Scan the qr code below to follow the wechat public account Xiao Li Budu, together with efficient learning Java.