Spring体系面试(spring、spring mvc、spring boot、spring cloud)

Foreword

Github:https://github.com/yihonglei/thinking-in-spring

CGLIB and a JDK dynamic proxy principle

CGLIB and JDK dynamic proxies difference

1, JDK dynamic proxies

Using interceptors (interceptor must implement InvocationHanlder) plus reflection Generates a proxy interface anonymous class,

InvokeHandler to handle the call before the call to a specific method.

2, CGLIB dynamic proxy

ASM using open source packages, class files proxy object classes loaded in, processed by modifying a subclass bytecode.

3, when to use JDK or CGLIB?

1) If the target object implements the interface, will use the default JDK dynamic proxy AOP implementation case.

2) If the target object implements the interface, you can force the use of CGLIB achieve AOP.

3) If the target object does not implement the interfaces must be used CGLIB library, Spring will automatically switch between JDK dynamic proxies and CGLIB.

4, how to force CGLIB achieve AOP?

1) was added CGLIB libraries (aspectjrt-xxx.jar, aspectjweaver-xxx.jar, cglib-nodep-xxx.jar)

2) adding Spring configuration file <aop: aspectj-autoproxy proxy-target-class = "true" />

5, JDK dynamic proxy and bytecode generation CGLIB difference?

1) JDK dynamic proxy class that implements the interface can only build agent, and not for the class.

2) CGLIB class is directed to a method implemented agents, mainly for generating a specified class subclasses, wherein the cover,

     And wherein the method for enhanced coverage, but because the use of inheritance, class or method is best not to be declared as final,

     For the final class or method can not be inherited.

6, CGlib faster than JDK?

1) Use CGLib dynamic proxy, the ASM CGLib bottom frame byte code generation, use the proxy class byte code generation techniques,

Before jdk6 higher than using the Java reflection efficiency. The only caveat is that, CGLib final method can not be declared as a proxy for,

Because CGLib principle is dynamically generated proxy class is a subclass of.

2) After jdk6, jdk7, jdk8 gradual optimization of the JDK dynamic proxy, in the case of a smaller number of calls, more efficient than CGLIB JDK proxy agent efficiency,

Only when a lot of call time, jdk6 and jdk7 CGLIB agent efficiency than a little lower, but to jdk8 time, jdk agency more efficient than CGLIB agent,

In short, every version upgrade jdk, jdk agent efficiency have been improved, and the news agency CGLIB really keep up the pace a little.

7, Spring or how to choose a JDK CGLIB?

1) When implementing an interface Bean, Spring will use the JDK dynamic proxy.

2) When Bean does not implement the interface, Spring use CGlib is achieved.

3) can force CGlib (added in the spring configuration <aop: aspectj-autoproxy proxy-target-class = "true" />).

 

Continuously updated in ......

 

Published 502 original articles · won praise 358 · Views 1.18 million +

Guess you like

Origin blog.csdn.net/yhl_jxy/article/details/105349760