Java Static proxy and Dynamic Proxy summary

  • 2020-06-12 09:01:28
  • OfStack

Static agent

The first implementation (based on interface) :

1 "interface


public interface Hello {
 void say(String msg);
}

2. Target class to implement at least 1 interface


public class HelloImpl implements Hello {
 public void say(String msg) {
  System.out.println("Hi,"+msg);
 }
}

3) Proxy class (implement the same interface as the target class, thus ensuring function 1)


public class HelloProxy implements Hello{
 private Hello hello;
 public HelloProxy(Hello hello){
  this.hello = hello;
 }
 public void say(String msg){
  before();
  hello.say(msg);
  after();
 }
 private void before(){
  System.out.println("Before");
 }
 private void after(){
  System.out.println("After");
 }
}

3 test


/**
 * @Author LZHL
 * @Create 2017-02-19 10:26
 * @Description
 */
public class Main {
 public static void main(String[] args) throws Exception {
  HelloImpl target = new HelloImpl();
  HelloProxy proxy = new HelloProxy(target);
  proxy.say("LZHL");
 }
}

The second implementation (based on the target class) :

1 > The target class


public class HelloTarget {
 public void sayHello(String name){
  System.out.println("Hi,"+name);
 }
}

2 > Proxy class (guaranteed function 1 by inheriting from the target class)


public class HelloProxy extends HelloTarget{
  private HelloTarget target;
  public HelloProxy(HelloTarget target){
    this.target = target;
  } 
  @Override
 public void sayHello(String name) {
  this.before();
  target.sayHello(name);
  this.after();
 }
 private void before(){
  System.out.println("Before");
 }
 private void after(){
  System.out.println("After");
 }
}

3 > test


public class Main {
 public static void main(String[] args) throws Exception {
  HelloTarget target = new HelloTarget(); 
    HelloProxy proxy= new HelloProxy(target);
  proxy.sayHello("LZHL");
 }
}

A dynamic proxy

The agent class of dynamic proxy is generated dynamically during program running, and there are two implementations, one is JDK dynamic proxy and the other is CGLib dynamic proxy

1) JDK Dynamic proxy (based on the interface implementation, the same interface is implemented as the target class, thus ensuring function 1)


/**
 * @Author LZHL
 * @Create 2017-02-19 12:46
 * @Description
 */
public class Main {
 public static void main(String[] args){
  final HelloImpl target = new HelloImpl();
  Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
   /*
    * proxy:  Proxy objects 
    * method:  The method object of the target object 
    * args:  The parameters of the target object method 
    * return:  The return value of the target object method 
    */
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before");
    Object retValue = method.invoke(target, args);
    System.out.println("after");
    return retValue;
   }
  });
  Hello proxy = (Hello) proxyInstance;
  proxy.say("LYX");
  // Can put the InvocationHandler Extract it out and write it separately 1 I'm going to do it in the form of an inner class, just so you can see it 
  class JDKProxy implements InvocationHandler {
   private Object target;
   public JDKProxy(Object target){
    this.target = target;
   }
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    before();
    Object result = method.invoke(target, args);
    after();
    return result;
   }
   private void before(){
    System.out.println("Before");
   }
   private void after(){
    System.out.println("After");
   }
  }
  InvocationHandler ih = new JDKProxy(target);
  Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih);
  Hello proxy2 = (Hello) proxyInstance2;
  proxy2.say("LZHL");
 }
}

2. CGLib dynamic proxy (based on the target class, by inheriting the target class, thus guaranteeing the function 1), needs to import the ES45en-3.2.4.jar package

pom.xml


<dependencies>
 <!-- https://mvnrepository.com/artifact/cglib/cglib -->
 <dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>3.2.4</version>
 </dependency>
</dependencies>

1) the target class


public class Hi {
 public void sayHi(String msg){
  System.out.println("Hi,"+msg);
 }
}

2) test


public class HelloImpl implements Hello {
 public void say(String msg) {
  System.out.println("Hi,"+msg);
 }
}
0

Above is the site to you Java static agent and dynamic agent summary, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!


Related articles: