1, Static proxy
The proxy class and the proxy class inherit the same interface. The proxy class needs to be used as a parameter in the proxy class case, so it is called to the proxy class; Disadvantages: each proxy class needs to write a corresponding proxy class;
public class Target{ void run(){ System.out.println("run Target!"); } } public class Proxy extends Target{ private Target target; Proxy(Target target){ this.target = target; } @Override void run(){ System.out.println("proxy do something else!"); this.target.run(); } }
//use void test(){ Target target = new Target(); Proxy proxy = new Proxy(target); //Instead of calling the original method directly, call the proxy's method proxy.run(); }
2, Dynamic agent
1. jdk dynamic agent
The proxy class needs to inherit the invocationhandler excuse. The principle is also the base and interface, which is similar to the static proxy; Disadvantages: the proxied class without a write interface cannot be proxied. It is called through reflection, which is slower than direct call;
public interface TargetInter{ void run(); } public class Target implements TargetInter{ public void run(){ System.out.println("run Target!"); }//Welcome to Java development exchange sample: 909038429 } public class Myproxy implements InvocationHandler{ private Object obj; public Object bind(Object obj){ this.obj = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("jdk dynamic proxy"); method.invoke(obj, args); return null; } }
void test(){ Target target = new Target(); TargetInter myproxy = (TargetInter) new Myproxy().bind(target); myproxy.run(); } ---------------------------------------------------- test()result: jdk dynamic proxy run Target!
2. Dynamic agent CGLIB
Principle: the runtime dynamically generates a subclass of the proxy class (implemented by ASM bytecode processing framework), and the subclass rewrites all non final methods in the proxy class. In the subclass, the method interception technology is adopted to intercept the calls of all parent methods, and then the AOP logic is implanted. Disadvantages: the proxied class with final modification cannot be proxied
public class Target { public void run(){//Welcome to Java development exchange sample: 909038429 System.out.println("run Target!"); } } public class Myproxy { Object obj; public Object bind(final Object target){ this.obj = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(obj.getClass()); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable{ System.out.println("CGLIB dynamic proxcy"); Object res = method.invoke(target, args); return res; } } ); return enhancer.create(); } }
void test(){ Target target = new Target(); Target proxy = (Target) new Myproxy().bind(target); }
Note: the proxy target object cannot be an internal class (because the creation of an internal class depends on an external class). If it is an internal class, cglib proxy will obtain a parameter constructor (the parameter is an external class object, which can be realized by passing construction parameters if it is really necessary to proxy an internal class). Cglib proxy creates a target object with a default constructor by default. If the target object has a parameter constructor, Cglib needs to specify the parameters of the constructor when acting as a proxy, or there must be a default constructor on the target object, otherwise it throws an exception Superclass has no null constructors but no arguments were given (you can create a proxy class by passing the construction parameters)
3. spring proxy mechanism
jdk dynamic proxy is preferred. If the object has no interface, CGLIB proxy is used
Others: opening and closing principle: a software entity such as class, module and function should be open to extension and closed to modification. A software entity should change by extension, not by modifying existing code. Some high-frequency interview questions (all sorted into documents) collected in 2020 have a lot of dry goods, including detailed explanations of mysql, netty, spring, threads, spring cloud, jvm, source code, algorithm, etc., as well as detailed learning plan, interview question sorting, etc. for friends who need to obtain these contents, please add Q sample: 909038429 /. / * welcome to join the java exchange Q Jun sample: 909038429, blowing water and chatting together