接口
public interface ICeo {
void meeting(String name) throws InterruptedException;
}
目标类
public class Ceo implements ICeo{
public void meeting(String name) throws InterruptedException {
Thread.sleep(2000);
System.out.println("和"+name+"谈成了34亿的项目");
}
}
代理类
public class CeoProxy implements ICeo{
private Ceo ceo;
public CeoProxy(Ceo ceo) {
this.ceo = ceo;
}
@Override
public void meeting(String name) throws InterruptedException {
System.out.println("秘书登记来访: "+name);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ceo.meeting(name);
stopWatch.stop();
double totalTimeMillis = stopWatch.getTotalTimeSeconds();
System.out.println("耗时: "+totalTimeMillis);
}
}
测试
public class CeoTest {
public static void main(String[] args) throws InterruptedException {
Ceo ceo = new Ceo();
CeoProxy ceoProxy = new CeoProxy(ceo);
ceoProxy.meeting("XXX");
}
}
接口
public interface IDao {
void select();
}
目标类
public class UserDao implements IDao{
@Override
public void select() {
try {
Thread.sleep(2000);
System.out.println("select ----");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public class JdkProxy implements InvocationHandler {
private Object target;
public JdkProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
StopWatch stopWatch = new StopWatch();
System.out.println("前置方法");
stopWatch.start();
Object obj = method.invoke(target, args);
stopWatch.stop();
System.out.println("后置方法");
System.out.println("耗时: "+stopWatch.getTotalTimeSeconds());
return obj;
}
}
测试
public class DaoTest {
public static void main(String[] args) {
UserDao userDao = new UserDao();
IDao dao = (IDao) Proxy.newProxyInstance(UserDao.class.getClassLoader(), UserDao.class.getInterfaces(), new JdkProxy(new UserDao()));
dao.select();
}
}
总结:
目标类
public class ZlmDao {
public void select(){
try {
Thread.sleep(2000);
System.out.println("select ---");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
代理类
public class CglibProxy implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
System.out.println("前置增强");
Object result = proxy.invokeSuper(obj, args);
stopWatch.stop();
System.out.println("后置增强");
System.out.println("耗时: "+stopWatch.getTotalTimeSeconds());
return result;
}
}
测试
public class ZlmDaoTest {
public static void main(String[] args) {
//实例化增强器
Enhancer enhancer = new Enhancer();
//设置需要代理的目标类
enhancer.setSuperclass(ZlmDao.class);
//设置拦截对象回调的实现类
enhancer.setCallback(new CglibProxy());
//生成代理类
ZlmDao zlmDao = (ZlmDao) enhancer.create();
zlmDao.select();
}
}
总结