Article Source: http://ms.csdn.net/geek/236074
1. Proxy mode
Proxy is a design pattern that provides additional access to the target object, that is, access to the target object through the proxy object. The advantage of this is that you can enhance the function of the target object by enhancing the functionality of the object. Here is a thought that is used in programming: Do not feel free to modify the code or methods that others have written, if you need to change, you can extend the method by proxy way
For example, to illustrate the role of the agent: Suppose we want to invite a star, then not directly connect the star, but contact the star's agent, to achieve the same goal. A star is a target, and he only takes charge of the program in the event, and other trivial things to his agent (broker) To solve. This is an example of the idea of acting in reality.
The diagram shows the following:
The key point of the proxy mode is: The proxy object and the target object. The proxy object is an extension to the target object and the target object is called
1.1. Static Proxy
Static proxies, when used, need to define interfaces or parent classes that are implemented with the same interface or inherit the same parent class as the proxy object.
Here's an example to explain:
To simulate a save action, define an interface to save the action: Iuserdao.java, and then the target object implements the method Userdao.java for this interface, at this point, if the static proxy method is used, the proxy object (Userdaoproxy.java) The Iuserdao interface is also implemented in the call. Invokes the target object by invoking the proxy object's method.
It is important to note that the proxy object is to implement the same interface as the target object, and then invoke the method of the target object by calling the same method
code example: interface: Iuserdao.java
/** * 接口 */public interface IUserDao { void save();}
Target object: Userdao.java
/** * 接口实现 * 目标对象 */public class UserDao implements IUserDao { public void save() { System.out.println(); }}
Proxy object: Userdaoproxy.java
/** * 代理对象,静态代理 */public class UserDaoProxy implements IUserDao{ //接收保存目标对象 private IUserDao target; public UserDaoProxy(IUserDao target){ this.target=target; } public void save() { System.out.println(); target.save();//执行目标对象的方法 System.out.println(); }}
Test class: App.java
/** * 测试类 */public class App { public static void main(String[] args) { //目标对象 UserDao target = new UserDao(); //代理对象,把目标对象传给代理对象,建立代理关系 UserDaoProxy proxy = new UserDaoProxy(target); proxy.save();//执行的是代理的方法 }}
Static Agent Summary:
1. You can extend the target function without modifying the function of the target object.
2. Disadvantages:
- Because the proxy object needs to implement the same interface as the target object, there are many proxy classes and too many classes. At the same time, once the interface is incremented, both the target object and the proxy object are maintained.
How to solve the disadvantages of static agents? The answer is that you can use dynamic proxy mode
1.2. Dynamic Agent
Dynamic agents have the following characteristics:
1. Proxy object, do not need to implement interface
2. The generation of proxy objects is the use of the JDK API to dynamically build proxy objects in memory (requires us to specify the type of interface that creates the proxy object/target object implementation)
3. Dynamic agent is also called: JDK Agent, Interface agent
API for generating proxy objects in JDK
Agent Class Package: Java.lang.reflect.Proxy
The JDK implementation agent only needs to use the Newproxyinstance method, but the method needs to receive three parameters, the complete wording is:
static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h )
Note that this method is a static method in the proxy class, and the three parameters received are:
- ClassLoader Loader,: Specifies that the current target object uses the class loader, and that the method to get the loader is fixed
- Class
/** * 创建动态代理对象 * 动态代理不需要实现接口,但是需要指定接口类型 */public class ProxyFactory{ //维护一个目标对象 private Object target; public ProxyFactory(Object target){ this.target=target; } //给目标对象生成代理对象 public Object getProxyInstance(){ return Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println(); //执行目标对象方法 Object returnValue = method.invoke(target, args); System.out.println(); return returnValue; } } ); }}
Test class: App.java
/** * 测试类 */public class App { public static void main(String[] args) { // 目标对象 IUserDao target = new UserDao(); // 【原始的类型 class cn.itcast.b_dynamic.UserDao】 System.out.println(target.getClass()); // 给目标对象,创建代理对象 IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance(); // class $Proxy0 内存中动态生成的代理对象 System.out.println(proxy.getClass()); // 执行方法 【代理对象】 proxy.save(); }}
Summarize:
The proxy object does not need to implement the interface, but the target object must implement the interface, otherwise it cannot use the dynamic proxy
1.3.Cglib Proxy
Both the static agent and the dynamic proxy pattern above require that the target object be the target object to implement an interface, but sometimes the target object is just a separate object, and there is no interface to implement, this can be used to implement the proxy in the same way as the object subclass, this method is called: Cglib agent
Cglib Proxy, also known as the subclass Proxy, is to build a subclass object in memory to extend the functionality of the target object.
- The dynamic proxy for JDK has a limitation that an object using a dynamic proxy must implement one or more interfaces, and if you want to broker a class that does not implement an interface, you can use the Cglib implementation.
- Cglib is a powerful, high-performance code-generation package that extends Java classes and implements Java interfaces at run time. It is widely used by many AOP frameworks, such as spring AOP and SYNAOP, to provide them with methods of interception (interception)
- The bottom of the Cglib package is to convert bytecode and generate new classes by using a small block of bytecode processing framework ASM. Direct use of ASM is discouraged because it requires you to be familiar with the format and instruction set of the JVM's internal structure including the class file.
Cglib sub-Class proxy implementation method:
1. The Cglib jar file needs to be introduced, but the Cglib feature is already included in spring's core package, so the Pring-core-3.2.5.jar can be introduced directly.
2. Once the feature Pack is introduced, you can dynamically build subclasses in memory
3. The proxy class cannot be final, otherwise the error
4. If the target object's method is final/static, it will not be intercepted, i.e. the target object's additional business methods will not be executed.
code example:
Target object class: Userdao.java
/** * 目标对象,没有实现任何接口 */public class UserDao { public void save() { System.out.println(); }}Java学习交流QQ群:589809992 我们一起学Java!
Cglib Agent Factory: Proxyfactory.java
/** * Cglib子类代理工厂 * 对UserDao在内存中动态构建一个子类对象 */public class ProxyFactory implements MethodInterceptor{ //维护目标对象 private Object target; public ProxyFactory(Object target) { this.target = target; } //给目标对象创建一个代理对象 public Object getProxyInstance(){ //1.工具类 Enhancer en = new Enhancer(); //2.设置父类 en.setSuperclass(target.getClass()); //3.设置回调函数 en.setCallback(this); //4.创建子类(代理对象) return en.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println(); //执行目标对象的方法 Object returnValue = method.invoke(target, args); System.out.println(); return returnValue; }}
Test class:
/** * 测试类 */public class App { @Test public void test(){ //目标对象 UserDao target = new UserDao(); //代理对象 UserDao proxy = (UserDao)new ProxyFactory(target).getProxyInstance(); //执行代理对象的方法 proxy.save(); }}
In spring's AOP programming:
If the target object that joins the container has an implementation interface, use the JDK proxy
If the target object does not implement an interface, use the Cglib proxy
Three types of proxy modes in Java