Analysis of java Dynamic proxy

Source: Internet
Author: User

Analysis of java Dynamic proxy
Recently, the company saw the use of MapperScannerConfigurer in the integration of mybatis and spring. This class automatically generates an interface-based Dynamic proxy class through reverse proxy. So I remembered the dynamic proxy of java, and then I had this article. This article uses a dynamic proxy to simulate the transaction blocker. Interface: public interface UserService {public void addUser (); public void removeUser (); public void searchUser ();} implementation class: public class UserServiceImpl implements UserService {public void addUser () {System. out. println ("add user");} public void removeUser () {System. out. println ("remove user");} public void searchUser () {System. out. println ("search user") ;}} there are two ways to implement java Dynamic Proxy: 1. jdk built-in dynamic proxy is required to use jdk built-in dynamic proxy The InvocationHandler interface and Proxy class are all in the java. lang. reflect package. InvocationHandler Introduction: InvocationHandler is the interface implemented by the call handler of the proxy instance. Each proxy instance has an associated InvocationHandler. When you call a method on a proxy instance, this method calls the InvocationHandler's invoke method. Proxy Introduction: Proxy provides static methods for creating dynamic Proxy classes and instances. Instance (simulate AOP Transaction Processing): public class TransactionInterceptor implements InvocationHandler {private Object target; public void setTarget (Object target) {this.tar get = target ;} @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. out. println ("start Transaction"); method. invoke (target, args); System. out. println ("end Transaction"); return null ;}} test code: public Class TestDynamicProxy {@ Test public void testJDK () {TransactionInterceptor transactionInterceptor = new TransactionInterceptor (); UserService userService = new UserServiceImpl (); transactionInterceptor. setTarget (userService); UserService userServiceProxy = (UserService) Proxy. newProxyInstance (userService. getClass (). getClassLoader (), userService. getClass (). getInterfaces (), transactionIntercepto R); userServiceProxy. addUser () ;}} Test Result: start Transactionadd userend Transaction is obvious. When we call a method through the userServiceProxy proxy class, the Transaction is enabled and closed before and after the method call. 2. The third-party library cglib CGLIB is a powerful, high-performance, high-quality code generation library that is used to expand Java classes and implement Java interfaces at runtime. The biggest difference between it and JDK dynamic proxy is that JDK dynamic proxy is for interfaces, while cglib is for classes to implement proxy, the principle of cglib is to generate a subclass for the specified target class and overwrite the method to implement enhancement. However, because the class is inherited, the final modified class cannot be used as a proxy. Instance: public class UserServiceCallBack implements MethodInterceptor {@ Override public Object intercept (Object o, Method method, Object [] args, MethodProxy methodProxy) throws Throwable {System. out. println ("start Transaction by cglib"); methodProxy. invokeSuper (o, args); System. out. println ("end Transaction by cglib"); return null ;}} Test code: public class TestDynamicProxy {@ Test public void testCGLIB () {Enhancer enhancer = new Enhancer (); enhancer. setSuperclass (UserServiceImpl. class); enhancer. setCallback (new UserServiceCallBack (); UserServiceImpl proxy = (UserServiceImpl) enhancer. create (); proxy. addUser () ;}} Test Result: start Transaction by cglibadd userend Transaction by cglib

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.