何为动态代理?为什么要用动态代理?动态代理怎么用?我们就带着这几个问题一起来去一探究竟吧!
要想知道何为动态代理,我们先来个静态代理!一个小小例子给你想要的答案!
public interface Beauty{
public void save();
}
public class Beauty implements Beauty{
public void save(){
Sysem.out.println("一个漂亮的美眉!");
}
}
public class BeautyProxy implements Beauty{
private Beauty target;
public void setTarget(Beauty o){
traget=o;
}
public Beauty getTarget(){
return target;
}
public void save(){
Sysem.out.println("前面是色狼!");
target.save();
Sysem.out.println("后面是色狼!");
}
}
public class TestProxy{
public static void main(String [] args){
Beauty b=new Beauty();
Beauty bproxy=new BeautyProxy
bproxy.setBeauty(b);
bproxy.save();
}
}
public interface UserManager{
public void save();
}
public class UserMangerImpl extends{
public void save(){
System.out.println("我要一个小妹妹!");
}
}
public class Transaction implements InvocationHandler{
private Object target;
public Transaction(Object ob){
targset=ob;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("找妹妹之前先洗洗吧!");
method.invoke(target, args);
System.out.println("我看你还是别找妹妹了,洗洗睡吧!");
return proxy;
}
public class Proxy
}