Home  >  Article  >  Java  >  Detailed explanation of code cases for Java reflection to implement Aop proxy

Detailed explanation of code cases for Java reflection to implement Aop proxy

黄舟
黄舟Original
2017-03-27 10:39:391380browse

This article mainly introduces Java reflection to implement Aop proxy. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look.

Use reflection to generate the dynamic proxy of JDK, which is the AOP proxy in AOP, replacing the target object, thereby weaving enhancements into the code.

Define proxy interface

Since JDKf dynamic proxy can only create dynamic proxies for interfaces, define the interface first. Assume that we need to add transaction processing to the Save method of the data. We have a UserDao interface, which has a Save method. The code is as follows:

public interface UserDao {
  public void save();
}

Define the proxy implementation

The following is the specific implementation of the Save method defined by the interface. We use the following code to achieve this.

public class UserDaoImpl implements UserDao {

  @Override
  public void save() {
    System.out.println("I am save user....");
  }

}

Define the enhancement code

We have the following operations, open the transaction before saving the user, commit the transaction after saving the user, defined in the enhancement code The two methods before() and after() are used before and after the execution of the save() method starts.

public class UserTx {
  public void before(){
    System.out.println("before save.....");
  }
  public void after(){
    System.out.println("after save......");
  }
}

Define Invocation handler

The reason why handler is defined is because when executing a dynamic proxy, what is actually executed is the invoke() method in the handler, so If so, we customize the content of the method in the invoke() method, thereby achieving the logic and effects of proxy and enhancement.

public class UserDaoInvocationHandler implements InvocationHandler {
  /
   需要代理的对象
   /
  private Object proxyObj;
  /
   指定我们需要代理的对象
   @param proxyObj
   */
  public void setProxyObj(Object proxyObj) {
    this.proxyObj = proxyObj;
  }
  @Override
  public Object invoke(Object proxy, Method method, Object[] args)
      throws Throwable {
    UserTx tx = new UserTx();
    tx.before();
    Object resultObj = method.invoke(proxyObj, args);
    tx.after();
    return resultObj;
  }
}

Test results

Everything has been defined above, we will actually dynamically proxy the object we specified and use the proxy object to execute The method we want to execute is to verify whether the proxy is successful.

import java.lang.reflect.Proxy;
public class ProxyTst {
  public static void main(String[] args) {
    // proxy object
    UserDao target = new UserDaoImpl();
    // invocation handler
    UserDaoInvocationHandler handler = new UserDaoInvocationHandler();
    handler.setProxyObj(target);
    // proxy
    UserDao targeted = (UserDao) Proxy.newProxyInstance(
        target.getClass().getClassLoader(),
        target.getClass().getInterfaces(),
        handler);
    // execute proxyed object
    targeted.save();
  }
}

After executing the above code, we can see the following output on the console, proving that UserDao has been successfully proxied, and we have also successfully added transaction functions to our program.

before save.....
I am save user....
after save......

The above is the detailed content of Detailed explanation of code cases for Java reflection to implement Aop proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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