java 代理之静态代理实践

需求描述:在没有源代码的情况下,只针对指定的类特定方法做增强(在原有功能不变的情况下新增功能),此时可以考虑使用静态代理

  1. 继承方式实现静态代理
    1.1实现描述:继承目标类,重写其方法,在方法中通过super调用其父类方法,并添加自己的增强的逻辑
    在这里插入图片描述

    1.2实现方式:
    代码中添加StudentDaoImpl类,并添加study方法

package com.yf.cn.dao;

public class StudentDaoImpl {
    public void study(String name){
        System.out.println(name+"正在学习");
    };
}

新增StudentDaoImplV1类,继承StudentDaoImpl,并重写study方法
package com.yf.cn.dao;

public class StudentDaoImplV1 extends StudentDaoImpl {
    @Override
    public void study(String name) {
        System.out.println("数据库保存操作日志");
        super.study(name);
    }
}

添加测试类进行测试

package com.yf.cn.test;


import com.yf.cn.dao.*;
import com.yf.cn.proxy.ProxyUtil;
import com.yf.cn.util.LuBanInvocationHandler;
import com.yf.cn.util.TestCustomHandle;

import java.lang.reflect.Proxy;

package com.yf.cn.test;


import com.yf.cn.dao.*;
import com.yf.cn.proxy.ProxyUtil;
import com.yf.cn.util.LuBanInvocationHandler;
import com.yf.cn.util.TestCustomHandle;

import java.lang.reflect.Proxy;

public class Test {
    public static void main(String[] args) throws Exception {

        //测试静态代理继承方式
        StudentDaoImpl studentDao = new StudentDaoImpl();
        studentDao.study("张三");
        System.out.println("--------------------------");
        StudentDaoImplV1 studentDaoImplV1 = new StudentDaoImplV1();
        studentDaoImplV1.study("张三");
    }
}


执行测试方法,可以看到目标类和代理类执行的方法,代理类已经在缘由基础上做了增强

在这里插入图片描述

  1. 聚合接口方式实现静态代理
    2.1实现描述:定义接口,然后再定义接口的实现类 ,然后定义代理类,代理类中定义构造方法传目标对象
    2.2定义CommonDao
package com.yf.cn.dao;

public interface CommonDao {
    public void getCuurrentDate();

    public String  sayhello(String name);
}

2.3定义CommonDao 的两个实现类CommonDaoImpl01、CommonDaoImpl02
CommonDaoImpl01:

package com.yf.cn.proxy1;

import com.yf.cn.dao.CommonDao;

import java.util.Date;

public class CommonDaoImpl01 implements CommonDao {
    @Override
    public void getCuurrentDate() {
        System.out.println(new Date().toString()+"===01");
    }

    @Override
    public String sayhello(String name) {
        System.out.println(name);
        return "01";
    }
}

CommonDaoImpl02:

package com.yf.cn.proxy1;

import com.yf.cn.dao.CommonDao;

import java.util.Date;

public class CommonDaoImpl02 implements CommonDao {
    @Override
    public void getCuurrentDate() {
        System.out.println(new Date().toString()+"02");
    }

    @Override
    public String sayhello(String name) {
        System.out.println("你好啊02"+name);
        return "success";
    }
}

2.4定义代理类CommonDaoImplProxy实现CommonDao 接口并通过构造方法构造CommonDao

package com.yf.cn.proxy1;

import com.yf.cn.dao.CommonDao;

public class CommonDaoImplProxy implements CommonDao {
    private CommonDao commonDao;
    public CommonDaoImplProxy(CommonDao commonDao){
        this.commonDao = commonDao;
    }
    @Override
    public void getCuurrentDate() {
        System.out.println("getCuurrentDate保存日志中01");
        commonDao.getCuurrentDate();
    }

    @Override
    public String sayhello(String name) {
        System.out.println("sayhello保存日志中");
        commonDao.sayhello(name);
        return "v1";
    }
}

2.5添加测试类进行测试

package com.yf.cn.test;


import com.yf.cn.dao.*;
import com.yf.cn.proxy1.CommonDaoImpl01;
import com.yf.cn.proxy1.CommonDaoImplProxy;
import com.yf.cn.proxy1.CommonDaoImpl02;

public class Test {
    public static void main(String[] args) throws Exception {
        CommonDao commonDao = new CommonDaoImplProxy(new CommonDaoImpl01());
        commonDao.getCuurrentDate();
        commonDao.sayhello("张永年");

    }
}

对CommonDaoImpl01(目标对象)的方法进行增强,增强得逻辑就是CommonDaoImplProxy中重写对应的方法中添加的逻辑
在这里插入图片描述
getCuurrentDate保存日志中01、sayhello保存日志中为增强逻辑中输出的内容
Tue Jan 25 14:20:22 CST 2022===01、张永年为目标方法调用输出的内容

java代理之动态代理实践(二)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值