动态代理的实现

动态代理: 可以在程序的执行过程中,创建代理对象。
通过代理对象执行方法,给目标类的方法增加额外的功能(功能增强)

jdk动态代理实现步骤:
1.创建目标类, SomeServiceImpl目标类,给它的doSome, doOther增加 输出时间, 事务。
2.创建InvocationHandler接口的实现类, 在这个类实现给目标方法增加功能。
3.使用jdk中 类Proxy,创建代理对象。 实现创建对象的能力。

 

1.创建SomeService类和它的实现类

package com.bjpowernode.service;

public interface SomeService {

    void doSome();
    void doOther();
}
package com.bjpowernode.service.impl;

import com.bjpowernode.service.SomeService;
import com.bjpowernode.util.ServiceTools;

//Service类不修改,也能够增加输出时间和事务的功能
public class SomeServiceImpl implements SomeService {
    @Override
    public void doSome() {

        ServiceTools.doLog();

        System.out.println("执行业务方法doSome");

        ServiceTools.doTrans();

    }

    @Override
    public void doOther() {
        ServiceTools.doLog();

        System.out.println("执行业务方法doOther");

        ServiceTools.doTrans();
    }
}

2. 工具类中实现与业务方法无关的工具类方法

package com.bjpowernode.util;

import java.util.Date;

public class ServiceTools {

    public static void doLog(){
        System.out.println("非业务方法 方法执行时间:"+new Date());
    }

    public static void doTrans(){
        //方法的最后,提交事务
        System.out.println("非业务方法,方法执行完之后,提交事务");
    }
}

3. 创建InvocationHandler接口的实现类, 在这个类实现给目标方法增加功能

package com.bjpowernode.handler;

import com.bjpowernode.util.ServiceTools;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    //目标对象
    private Object target;//SomeServiceImpl类

    public MyInvocationHandler(Object target) {
        this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //通过代理对象执行方法时,会调用执行这个invoke()
        System.out.println("执行MyInvocationHandler中的invoke方法");
        System.out.println(method.getName());
        Object res = null;
        ServiceTools.doLog();
        //执行目标类的方法,通过Method类实现
        res = method.invoke(target,args);//SomeServiceImpl的doSome()和doOther()方法
        ServiceTools.doTrans();
        //目标方法执行结果
        return res;
    }
}

4.使用jdk中类Proxy,创建代理对象。 实现创建对象的能力

package com.bjpowernode;

import com.bjpowernode.handler.MyInvocationHandler;
import com.bjpowernode.service.SomeService;
import com.bjpowernode.service.impl.SomeServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class MyApp {

   public static void main(String[] args) {

       /*SomeService someService = new SomeServiceImpl();
       someService.doSome();
       someService.doOther();*/

       //使用jdk的Proxy创建代理对象
       //创建目标对象
       SomeService target = new SomeServiceImpl();

       //创建InvocationHandler对象
       InvocationHandler invocationHandler = new MyInvocationHandler(target);

       //使用Proxy创建代理
       SomeService proxy = (SomeService)Proxy.newProxyInstance(target.getClass().getClassLoader(),
               target.getClass().getInterfaces(),invocationHandler);

       //通过代理执行方法,会使用handler中的invoke()
       proxy.doSome();

    }
}

5.结果

6.对不同的方法增加额外的功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值