本文章主要讲解以下几个点:
1、代理模式
2、静态代理
3、JDK的动态代理
4、CGLB动态代理
给某一个对象提供一个代理,并由代理对象控制对原对象的引用。
比如:一个人(客户端)需要去租房子,但是找不到房子,就会去找中介(代理对象)租房子,真实的房子主人(真实对象)并不会被客户端知晓。所有的租房子操作都是由中介代理办理。
在程序运行之前就已经存在代理类的字节码文件,代理对象和真实对象的关系在程序运行之前就确定了。(代理对象需要我们自己去手动创建)
org.springframework spring-context 5.0.8.RELEASE org.springframework spring-test 5.0.8.RELEASE test junit junit 4.12 test
package cn.simplelife._01staticProxy;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/22 19:14* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}
package cn.simplelife._01staticProxy.impl;import cn.simplelife._01staticProxy.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/22 19:15* @Version 1.0*/public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存" + name);}
}
package cn.simplelife._01staticProxy.proxy;import cn.simplelife._01staticProxy.IEmployeeService;
import cn.simplelife._01staticProxy.tx.MyTransaction;
import org.springframework.stereotype.Component;/*** @ClassName IEmployeeProxy* @Description* @Author simplelife* @Date 2022/11/22 19:24* @Version 1.0*/
@Component
public class IEmployeeProxy implements IEmployeeService {private MyTransaction myTransaction; // 事务管理对象private IEmployeeService target; // 代理类中的真实类对象// 因为使用的是XML配置方式,所以这里必须提供Set方法public void setMyTransaction(MyTransaction myTransaction) {this.myTransaction = myTransaction;}public void setTarget(IEmployeeService target) {this.target = target;}@Overridepublic void save(String name, String password) {try {myTransaction.begin();// 真实类对象调用真实类的方法保存信息target.save(name, password);myTransaction.commit();} catch (Exception e) {myTransaction.rollback();e.printStackTrace();}}
}
package cn.simplelife._01staticProxy.proxy;import cn.simplelife._01staticProxy.IEmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/*** @ClassName IEmployeeProxyTest* @Description* @Author simplelife* @Date 2022/11/22 19:29* @Version 1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeProxyTest {@Autowiredprivate IEmployeeService iEmployeeService;@Testpublic void save() {System.out.println(iEmployeeService);iEmployeeService.save("张胜男","12315");}
}
Java 动态代理机制生成的所有动态代理类的父类,它提供了一组静态方法来为一组接口动态地生成代理类及其对象。
主要方法
public static Object newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler hanlder)
方法职责:为指定类加载器、一组接口及调用处理器生成动态代理类实例。
参数:
返回值:创建的代理对象。
public Object invoke(Object proxy, Method method, Object[] args)
方法职责:负责集中处理动态代理类上的所有方法调用,让使用者自定义做什么事情,对原来方法增强。
参数:
返回值:真实方法的返回结果。
org.springframework spring-context 5.0.8.RELEASE org.springframework spring-test 5.0.8.RELEASE test junit junit 4.12 test
package cn.simplelife._01staticProxy;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/22 19:14* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}
package cn.simplelife._01staticProxy.impl;import cn.simplelife._01staticProxy.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/22 19:15* @Version 1.0*/public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存" + name);}
}
package cn.simplelife.jdkproxy.handler;import cn.simplelife.jdkproxy.tx.MyTransactionManger;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/*** @ClassName IEmployeeServiceProxy* @Description* @Author simplelife* @Date 2022/11/22 19:55* @Version 1.0*/public class TransactionInvocationHandler implements InvocationHandler {private MyTransactionManger myTransactionManger; // 事务管理器private Object target; // 真实对象public void setMyTransactionManger(MyTransactionManger myTransactionManger) {this.myTransactionManger = myTransactionManger;}public void setTarget(Object target) {this.target = target;}public Object getTarget() {return target;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object invoke = null;try {myTransactionManger.begin();// 真实对象调用方法invoke = method.invoke(target, args);myTransactionManger.commit();} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {myTransactionManger.rock();e.printStackTrace();}return invoke;}
}
package cn.simplelife.jdkproxy.handler;import cn.simplelife.jdkproxy.IEmployeeService;
import cn.simplelife.jdkproxy.impl.IEmployeeServiceImpl;
import cn.simplelife.jdkproxy.tx.MyTransactionManger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.lang.reflect.Proxy;/*** @ClassName IEmployeeServiceProxyTest* @Description* @Author simplelife* @Date 2022/11/22 19:58* @Version 1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceProxyTest {@Autowiredprivate TransactionInvocationHandler invocationHandler;@Testpublic void testJDKProxy() {invocationHandler.setMyTransactionManger(new MyTransactionManger());invocationHandler.setTarget(new IEmployeeServiceImpl());IEmployeeService proxyInstance = (IEmployeeService) Proxy.newProxyInstance(this.getClass().getClassLoader(),invocationHandler.getTarget().getClass().getInterfaces(),invocationHandler);proxyInstance.save("李四", "11345");}
}
类似 JDK 中 Proxy,用来生成代理类创建代理对象的。
类似 JDK 中 InvocationHandler,让使用者自定义做什么事情,对原来方法增强。
package cn.simplelife.cjlbproxy.impl;import cn.simplelife.cjlbproxy.IEmployeeService;
import cn.simplelife.cjlbproxy.handler.TransactionInvokeHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @ClassName IEmployeeServiceImplTest* @Description* @Author simplelife* @Date 2022/11/22 15:38* @Version 1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceImplTest {@Autowiredprivate TransactionInvokeHandler transactionInvokeHandler;@Testpublic void save() {Enhancer enhancer = new Enhancer();// 生产的代理类是继承真实类的enhancer.setSuperclass(transactionInvokeHandler.getTarget().getClass());// 生成的代理类对象要做的事情enhancer.setCallback(transactionInvokeHandler);// 接收生成的代理类IEmployeeService iEmployeeService = (IEmployeeService) enhancer.create();iEmployeeService.save("yyy", "123");}
}
上一篇:《茶馆》经典台词