动态代理实现

jdk动态代理

-- 接口
package com.bu.eurekaclientprovider.service;

public interface OrderService {

    String getOderId(String name) throws Exception;

}

-- 实现

package com.bu.eurekaclientprovider.service.impl.jdk;

import com.bu.eurekaclientprovider.service.OrderService;

public class IOderService implements OrderService {
    @Override
    public String getOderId(String name) throws Exception {
        System.out.println("方法正在执行");
        return "xxxx";
    }
}

```

```
-- 核心
package com.bu.eurekaclientprovider.service.impl.jdk;

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

public class JdkProxy implements InvocationHandler {

    private Object target;

    public Object getInstance(Object target) {
        this.target = target;
        Class clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(target, args);
        after();
        return invoke;
    }

    public void before() {
        System.out.println("任务执行之前");
    }

    public void after() {
        System.out.println("任务执行之后");
    }
}
```
方法调用
```
package com.bu.eurekaclientprovider.service.impl.jdk;

import com.bu.eurekaclientprovider.service.OrderService;

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

        OrderService jdkProxy = (OrderService) new JdkProxy().getInstance(new IOderService());

        jdkProxy.getOderId("测试");

    }
}

```

## cjlib 动态代理

package com.bu.eurekaclientprovider.service.impl.cjlib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CjlibProxy implements MethodInterceptor {

public Object getInstance(Class clazz) throws Exception {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(this);
    return enhancer.create();
}

@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    before();
    // invokeSuper
    Object obj = methodProxy.invokeSuper(o, objects);
    after();
    return obj;
}

public void before() {
    System.out.println("任务执行之前");
}

public void after() {
    System.out.println("任务执行之后");
}

}

package com.bu.eurekaclientprovider.service.impl.cjlib;

import com.bu.eurekaclientprovider.service.OrderService;
import com.bu.eurekaclientprovider.service.impl.jdk.IOderService;

public class ExeMain {

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

    IOderService instance = (IOderService) new CjlibProxy().getInstance(IOderService.class);

    try {
        instance.getOderId("ce");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

你可能感兴趣的:(动态代理实现)