Spring AOP implementation principle (3) Use CGLIB to generate proxy classes

Source: Internet
Author: User

Spring AOP implementation principle (3) Use CGLIB to generate proxy classes

CGLIB (Code Generation Library) is a class Library generated by Code. It dynamically generates subclass of a class at runtime.

The Chinese class defined above is used here. Now, CGLIB is used to generate a proxy directly. This proxy class can also achieve the effect of Spring AOP proxy.

The following provides an interceptor implementation class for CGLIB:
Public class AroundAdvice implements MethodInterceptor {public Object intercept (Object target, Method method, Object [] args, MethodProxy proxy) throws java. lang. throwable {System. out. println ("simulate start transaction before executing the target method... "); // execute the target method and save the returned Object rvt = proxy after the target method is executed. invokeSuper (target, new String [] {"changed parameter"}); System. out. println ("simulate the end of the transaction after the target method is executed... "); return rvt +" new content ";}}

The role of the above AroundAdvice. java is like the previous introduction of Around Advice, which can be woven into the enhancement processing before calling the target method and after calling the target method.

Next, the program provides a ChineseProxyFactory class. This ChineseProxyFactory class will generate a proxy class for Chinese via CGLIB:

 

Public class ChineseProxyFactory {public static Chinese getAuthInstance () {Enhancer en = new Enhancer (); // set the target class en to be proxy. setSuperclass (Chinese. class); // set the interceptor en. setCallback (new AroundAdvice (); // generate the proxy class instance return (Chinese) en. create ();}}

 

Run the above main program and see the following output:

 

Simulate start transaction before executing the target method... -- The sayHello method is being executed. After the target method is executed, the transaction is simulated and ended... the modified parameter Hello, before the newly added content of CGLIB executes the target method, simulate the start transaction... I am eating: After the modified parameter executes the target method, it simulates and ends the transaction .. class lee. chinese $ EnhancerByCGLIB $4bd097d9

 

According to the output above, the agent generated by CGLIB can be used as a Chinese object, and the sayHello () and eat () Methods of the CGLIB proxy object have added transaction control.

The CGLIB proxy is actually the AOP proxy generated by Spring AOP.

 

Through the final output of the program, it is not difficult to find that the implementation class of this proxy object is lee. Chinese $ EnhancerByCGLIB $4bd097d9, which is the proxy class generated by CGLIB. This proxy

The format of the class is exactly the same as that of the proxy class generated by Spring AOP.

 

This is the root of Spring AOP: Spring AOP dynamically generates proxy objects through CGLIB. This proxy object is the so-called AOP proxy, while the AOP proxy method

The target method is enhanced by dynamically weaving the enhancement processing into the target object's entry point.

 

Next, the program provides a ChineseProxyFactory class. This ChineseProxyFactory class will generate a proxy class for Chinese via CGLIB: The above bold text code is used

CGLIB Enhancer generates key code for the proxy object. At this time, Enhancer takes the Chinese class as the target class and the AroundAdvice object as the "Advice", and the program will generate

A subclass of Chinese. This subclass is the proxy class generated by CGLIB. It can be used as a Chinese object, but it enhances the Chinese class method.

 

The main program for testing the Chinese proxy class is as follows:

 

Public class Main {public static void main (String [] args) {Chinese chin = ChineseProxyFactory. getAuthInstance (); System. out. println (chin. sayHello ("Sun Wukong"); chin. eat ("watermelon"); System. out. println (chin. getClass ());}}

 

AOP is widely used to process system-level services with cross-cutting nature. The emergence of AOP is a good supplement to OOP, which allows developers to process cross-cutting services in a more elegant way.

Service. Regardless of the AOP implementation, AspectJ, or Spring AOP, they all need to dynamically generate an AOP proxy class. The difference is that the timing for generating the AOP proxy class is different:

AspectJ uses the AOP proxy class generated during compilation, so it has better performance, but it needs to be processed using a specific compiler. Spring AOP uses the runtime to generate the AOP proxy class,

Therefore, you do not need to use a specific compiler for processing. Because Spring AOP requires to generate an AOP proxy at each runtime, the performance is slightly lower.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.