When we use the general objects Aop, commonly dynamic proxy mode, i.e. using the mapping is the same class for a pre- and post-operation on this basis.
Dynamic proxies are mostly using the original parent class implements an interface, then a dynamic proxy class and the same original twin brothers classes to implement mapping.
father
public interface InterF { public void save(); }
You need to map classes
public class Origin implements InterF{ @Override public void save() { System.out.println("测试一下"); } }
Dynamic mapping agency, such need to pass the corresponding original class object
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author bai * @create 2019-10-25-16:47 */ public class ProxyJdk implements InvocationHandler { private Object target; public Object getProxy(Object target){ this.target=target; return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("我来了"); Object invoke = method.invoke(target, args); System.out.println("我走了"); return invoke; } }
Test category
public class Mytest { public static void main(String[] args) { Origin o=new Origin(); ProxyJdk pj=new ProxyJdk(); InterF proxy = (InterF) pj.getProxy(o); proxy.save(); } }
cglib mode dynamic proxy agents and similar, the same pattern, but is used directly to achieve cglib Sons class that inherits the class to implement the mapping.
The original class
public class Origin { public void Save () { System.out.println ( "test" ); } }
cglib mapping class, which also need to pass the original class
import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; /** * @author bai * @create 2019-10-25-17:26 */ public class CglibProxy implements MethodInterceptor{ private Object target;//被代理目标 public Object getProxy(Object target){ this.target=target; Enhancer enhancer = new Enhancer(); //1.设置基类 enhancer.setSuperclass (target.getClass ()); // 2. Set callback interface enhancer.setCallback ( the this ); // MethodInterceptor implementation class // Create dynamic proxy return enhancer.create (); } @Override public Object Intercept (Object O, Method, Method, Object [] Objects, methodProxy methodProxy) throws the Throwable { System.out.println ( "before preparation" ); Object Invoke = Method.invoke (target, Objects); System.out.println ( "preparation after " ); return Invoke; } }
o is an incoming object, method is the tangent point of the respective cutting methods, objects i.e. parameters.
Test category
public class Mytest2 { public static void main(String[] args) { Origin o=new Origin(); CglibProxy cp=new CglibProxy(); Origin proxy = (Origin) cp.getProxy(o); proxy.save(); } }
cglib dynamic proxies with respect to at least one parent, more convenient. And the corresponding need to import cglib jar package using