Home > Java > javaTutorial > Detailed explanation of two dynamic proxies, cglib and jdk

Detailed explanation of two dynamic proxies, cglib and jdk

Y2J
Release: 2017-05-12 09:50:10
Original
2022 people have browsed it

This article mainly introduces the relevant knowledge of spring cglib and jdk dynamic proxy, which has a good reference value. Let’s take a look at it with the editor

1. Overview

JDK dynamic proxy uses the java reflection mechanism to generate an anonymous class that implements the interface, and then calls the specific method Before calling InvocationHandler to process

Cglib dynamic proxy uses the asm open source package to load the class file of the proxy class and modify its bytecode to generate a subclass for processing

If the target object is implemented The interface uses jdk proxy by default (cglib proxy can be forced to be used)

If the interface is not implemented, cglib proxy must be used

Forcing the use of cglib proxy is required

*Introduce cglibjar package

*Configure spring   

cglib is a method of dynamically generating subclasses of the proxy class and overriding the proxy class. To achieve this, do not use finalmodification

2. Code example

2.1 cglib proxy class

package com.rocky.spring;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy {

  public static void main(String[] args) {
    final UserService service = new UserService();
    Enhancer hancer = new Enhancer();
    hancer.setSuperclass(service.getClass());
    hancer.setCallback(new MethodInterceptor(){

      @Override
      public Object intercept(Object proxy, Method method, Object[] arg2,
          MethodProxy arg3) throws Throwable {
        System.out.println("增强前 ... Cglib");
        Object invoke = method.invoke(service, arg2);
        System.out.println("增强后 ... Cglib");
        return invoke;
      }});
    UserService userService = (UserService) hancer.create();
    userService.sayHello();

  }
}
//需要引入cglib-2.2.jar 和org.objectweb.asm-3.3.1.jar 
//输出
//增强前 ... Cglib
//this userService works....
//增强后 ... Cglib
Copy after login

Proxied class UserService

package com.rocky.spring;

public class UserService {

  public void sayHello(){
    System.out.println("this userService works....");
  }
}
Copy after login

2.2 jdk proxy interface

package com.rocky.spring;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy {

  public static void main(String[] args) {
    final ActorService service = new ActorServiceImpl();
    ActorService actorService = (ActorService) Proxy.newProxyInstance(
        service.getClass().getClassLoader(), service.getClass()
            .getInterfaces(), new InvocationHandler() {
          @Override
          public Object invoke(Object proxy, Method method,
              Object[] args) throws Throwable {
            System.out.println("增强前...jdk");
            Object invoke = method.invoke(service, args);
            System.out.println("增强后...jdk");
            return invoke;
          }
        });
    actorService.sayHi();
  }
}
//增强前...jdk
//Honestly, I do the work.
//增强后...jdk
Copy after login

Proxied interface and Implementation class

package com.rocky.spring;
public interface ActorService {
  public void sayHi();
}
-----------------
package com.rocky.spring;
public class ActorServiceImpl implements ActorService {

  @Override
  public void sayHi() {
    doSomething();
  }

  private void doSomething() {
    System.out.println("Honestly, I do the work.");
  }
}
Copy after login

[Related recommendations]

1. Java Free Video Tutorial

2. JAVA Tutorial Manual

3. Comprehensive analysis of Java annotations

The above is the detailed content of Detailed explanation of two dynamic proxies, cglib and jdk. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template