动态代理 - wtdig/study GitHub Wiki

动态代理

一、jdk动态代理

1、代码
package cn.wt.jdkproxy.proxyfactory;

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

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午9:41:03
 */
public class JdkProxyFactory {

    // 目标对象的传递
    private Object target;

    public JdkProxyFactory(Object target){
        super();
        this.target = target;
    }

    public Object getJdkProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
                                      new InvocationHandler() {

                                          public Object invoke(Object proxy, Method method, Object[] args)
                                                                                                          throws Throwable {
                                              // 增强方法
                                              int sum = Cookie(args[0]);
                                              Object invoke = method.invoke(target, sum);
                                              return invoke;
                                          }

                                      });
    }

    // 增强的方法
    private int Cookie(Object count) {
        int counts = (Integer) count;
        counts++;
        System.out.println("jdkproxy增强了:" + counts);
        return counts;
    }
}


package cn.wt.jdkproxy.service;

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午9:36:41
 */
public interface LearnService {

    public void study(int count);
}


package cn.wt.jdkproxy.service.impl;

import cn.wt.jdkproxy.service.LearnService;

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午9:38:04
 */
public class LearnSeviceImpl implements LearnService {

    public void study(int count) {
        System.out.println("jdkproxy的学习内容" + count);
    }

}


package cn.wt.jdkproxy.test;

import cn.wt.jdkproxy.proxyfactory.JdkProxyFactory;
import cn.wt.jdkproxy.service.impl.LearnSeviceImpl;
import cn.wt.jdkproxy.service.LearnService;
import org.junit.Test;

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午9:39:37
 */
public class JdkProxyTest {

    @Test
    public void JdkProxy() {
        // 创建目标对象
        LearnService service = new LearnSeviceImpl();
        // 获取目标对象的代理对象
        JdkProxyFactory jdkProxyFactory = new JdkProxyFactory(service);
        LearnService proxy = (LearnService) jdkProxyFactory.getJdkProxyInstance();
        proxy.study(100);
    }
}

二、cglib动态代理

1、pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.wt.cglibproxy</groupId>
  <artifactId>cglibproxy</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.0.2.RELEASE</version>
      </dependency>
  </dependencies>
</project>
2、代码
package cn.wt.cglibproxy.proxyfactory;

import java.lang.reflect.Method;

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

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午10:28:25
 */
public class CglibProxyFactory {

    // 目标对象的传递
    private Object target;

    public CglibProxyFactory(Object target){
        super();
        this.target = target;
    }

    public Object getCglibProxyInstance() {
        Enhancer eh = new Enhancer();
        // 设置目标对象
        eh.setSuperclass(target.getClass());
        // 设置回调方法
        eh.setCallback(new MethodInterceptor() {

            public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
                // 增强方法
                int callCount = cglibEnhancer(arg2[0]);
                Object invoke = arg1.invoke(target, callCount);
                return invoke;
            }

        });
        // 返回代理对象
        return eh.create();
    }

    // 增强方法
    private int cglibEnhancer(Object count) {
        int sums = (Integer) count;
        sums++;
        System.out.println("cglib增强了" + sums);
        return sums;
    }
}

package cn.wt.cglibproxy.service;

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午10:25:22
 */
public class MarkService {

    public String mark(int sum) {
        System.out.println("mark开始进行过了:" + sum);
        return "origin mark";
    }
}


package cn.wt.cglibproxy.test;

import cn.wt.cglibproxy.proxyfactory.CglibProxyFactory;
import cn.wt.cglibproxy.service.MarkService;
import org.junit.Test;

/**
 * @author wb-wt261136
 * @version 2017年8月11日 上午10:39:43
 */
public class CglibProxyTest {

    @Test
    public void cglibTest() {
        // 获取目标对象
        MarkService target = new MarkService();
        // 获取代理对象
        CglibProxyFactory cglibProxyFactory = new CglibProxyFactory(target);
        MarkService proxy = (MarkService) cglibProxyFactory.getCglibProxyInstance();
        proxy.mark(100);
    }
}

⚠️ **GitHub.com Fallback** ⚠️