Let's talk about java proxy mode with you
1. Static proxy
1.1 Both the static proxy proxy and the proxy class must maintain a common interface.
public interface IUserDao { void save();}1.2 Proxy class, target object
public class UserDao implements IUserDao{ @Override public void save() { System.out.println("-----The data has been saved!!!------"); }}1.3 Proxy Objects
public class UserDaoProxy implements IUserDao{ // Receive and save the target object private IUserDao target; public UserDaoProxy(IUserDao target) { this.target = target; } @Override public void save() { System.out.println("Start transaction..."); target.save(); // Method to execute the target object System.out.println("Submit transaction..."); }}1.4 Test Class
public class App { public static void main(String[] args) { // Target object IUserDao target = new UserDao(); // Proxy IUserDao proxy = new UserDaoProxy(target); proxy.save(); // Execute the proxy method}} 2. Dynamic Agent
2.1 Similarly, dynamic proxy also needs to complete an interface. (Similar to above)
2.2 The target object is the same.
2.3 It's just different in proxy objects
public class ProxyFactory { // Maintain a target object private Object target; public ProxyFactory(Object target){ this.target = target; } // Generate a proxy object 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("Start transaction"); // Execute the target object method Object returnValue = method.invoke(target, args); System.out.println("Submit transaction"); return returnValue; } }); }}2.4 Test Class
public class App { public static void main(String[] args) { // Target object IUserDao target = new UserDao(); System.out.println(target.getClass()); // Create proxy object for the target object IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance(); System.out.println(proxy.getClass()); // Execute method [proxy object] proxy.save(); }} 3. cglib proxy
3.1cglib proxy does not need to complete the interface, it only needs to write the class and proxy class to be proxyed. The proxy here is the same as 1.2, so it is no longer written.
3.2 The proxy class is different. When using the cglib proxy mode, you need to refer to the core framework package of spring.
public class ProxyFactory implements MethodInterceptor{ // Maintain the target object private Object target; public ProxyFactory(Object target){ this.target = target; } // Create a proxy object for the target object public Object getProxyInstance(){ //1. Tool class Enhancer en = new Enhancer(); //2. Set the parent class en.setSuperclass(target.getClass()); //3. Set the callback function en.setCallback(this); //4. Create a subclass (proxy object) return en.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("Start transaction..."); // Method to execute target object Object returnValue = method.invoke(target, args); System.out.println("Submit transaction......"); return returnValue; }}3.3 Test Class
public class App { public static void main(String[] args) { // Target object UserDao target = new UserDao(); System.out.println(target.getClass()); // Proxy object UserDao proxy = (UserDao) new ProxyFactory(target).getProxyInstance(); System.out.println(proxy.getClass()); // Method to execute proxy object proxy.save(); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.