spring三种代理方式

spring-Proxy

  1. 代理方式一
public class client {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        Proxy proxy = new Proxy();
        proxy.setUserServiceImpl(userService);
        proxy.add();
    }

}
public class Proxy implements UserService{
    private UserServiceImpl userServiceimpl;

    public void setUserServiceImpl(UserServiceImpl userServiceimpl) {
        this.userServiceimpl = userServiceimpl;
    }

    @Override
    public void add() {
        log("add");
        userServiceimpl.add();
    }
    public void log(String msg){
        System.out.println("use " + msg + " method");
    }
}
public interface UserService {
    public  void add();
}
`
public class UserServiceImpl implements UserService{
   @Override
    public void add() {
        System.out.println("增加一个用户");
    }
  1. 代理方式二
public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        pih.setRent(host);
        Rent proxy = (Rent) pih.getProxy();
        proxy.rent();
    }
}
public class Host implements Rent{
    @Override
    public void rent() {
        System.out.println("房东出租房子");
    }
}
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的接口
    private Rent rent;
    public void setRent(Rent rent) {
        this.rent = rent;
    }
    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
    }
    //处理代理实例,并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现
        seeHouse();
        Object invoke = method.invoke(rent, args);
        sign();
        return invoke;
    }
    public void seeHouse(){
        System.out.println("中介带人看房子");
    }
    public void sign(){
        System.out.println("签合同");
    }
}
public interface Rent {
    public void rent();
}

  1. 代理方式三
public class Client {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        InvocationHandler pih = new InvocationHandler();
        pih.setTarget(userService);
        UserService proxy = (UserService) pih.getProxy();
        proxy.add();
    }
}

public class InvocationHandler implements java.lang.reflect.InvocationHandler {
    //被代理的接口
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    //处理代理实例,并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现
        log(method.getName());
        Object invoke = method.invoke(target, args);
        return invoke;
    }

    public void log(String msg){
        System.out.println("run "+msg+" method");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值