初识代理------如何将字符串转换成类

初识代理------如何将字符串转换成类

1.接口IndexDao
public interface IndexDao {
public void query();
public String queryString(String name,Integer age);
}

2.实现类IndexDaoImpl
public class IndexDaoImpl implements IndexDao {
public void query() {
System.out.println(“query”);
}

public String queryString(String name, Integer age) {
    System.out.println("proxy-string");
    return "";
}

}

3.通用类代码示例ProxyUtil:

public class ProxyUtil {

//获取代理对象---插入的逻辑固定
public static Object createProxy(Object target){

    String line = "\n";//换行
    String tab = "\t";//缩进
    Class targetClazz = target.getClass().getInterfaces()[0];//获取传入对象的接口

    String targetClazzName = targetClazz.getName();
    String targetClazzSimpleName = targetClazz.getSimpleName();

    //接口中的方法
    Method[] methods =  targetClazz.getDeclaredMethods();

    //类中的内容
    String content = "";
    //包声明
    String packageContent = "package com.proxy;"+line;

    //导入声明
    String importContent = "import "+targetClazzName+";"+line;
    //类定义
    String firstLineContent = "public class $Proxy implements "+targetClazzSimpleName+" {"+line;
    //属性
    String filedContent = tab+"private "+targetClazzSimpleName+" target;"+line;
    //构造方法
    String constructorContent = tab+"public $Proxy("+targetClazzSimpleName+" target){" +line+
                                tab+tab+"this.target = target;" +line+
                                tab+"}"+line;
    //方法实现
    String methodContent = "";

    for (Method method : methods) {
        String returnType = method.getReturnType().getSimpleName();//返回类型
        String methodName = method.getName();//方法名
        Class[] params = method.getParameterTypes();//参数类型
        int index = 0;
        String paramContent = "";
        String paramNames = "";
        //参数列表
        if(params != null && params.length > 0){
            for (int i=0;i

//获取代理对象升级–加入业务

public static Object createProxyNew(Class targetClazz, TestInvocationHandler th){

    String line = "\n";//换行
    String tab = "\t";//缩进

    String targetClazzName = targetClazz.getName();
    String targetClazzSimpleName = targetClazz.getSimpleName();

    String thSimpleName = th.getClass().getSimpleName();

    //接口中的方法
    Method[] methods =  targetClazz.getDeclaredMethods();

    //类中的内容
    String content = "";
    //包声明
    String packageContent = "package com.proxy;"+line;

    //导入声明
    String importContent = "import "+targetClazzName+";"+line+
                            "import java.lang.reflect.Method;"+line+
                            "import com.spring.dao.TestInvocationHandler;"+line;
    //类定义
    String firstLineContent = "public class $Proxy implements "+targetClazzSimpleName+" {"+line;
    //属性
    String filedContent = tab+"private TestInvocationHandler target;"+line;
    //构造方法
    String constructorContent = tab+"public $Proxy(TestInvocationHandler target){" +line+
            tab+tab+"this.target = target;" +line+
            tab+"}"+line;
    //方法实现
    String methodContent = "";

    for (Method method : methods) {
        String returnType = method.getReturnType().getSimpleName();//返回类型
        String methodName = method.getName();//方法名
        Class[] params = method.getParameterTypes();//参数类型
        int index = 0;
        String paramContent = "";
        String paramNames = "";
        String paramTypes = "";
        String paramArrays = "";
        //参数列表
        if(params != null && params.length > 0){
            paramTypes = ",";
            for (int i=0;i

}

4.模拟JDK代理接口

public interface TestInvocationHandler {
public Object invoke(Method method, Object[] args);
}

5.模拟JDK代理实现类

public class TestInvocationHandlerImpl implements TestInvocationHandler {

private Object target;

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

@Override
public Object invoke(Method method, Object[] args) {
    try {
        return method.invoke(target,args);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

}

6.测试类Test
public class Test {

public static void main(String[] args) {
 //自定义动态代理
    IndexDao indexDao = (IndexDao) ProxyUtil.createProxyNew(IndexDao.class,
            new TestInvocationHandlerImpl(new IndexDaoImpl()));
    indexDao.query();
    indexDao.queryString("ok",1);


    //jdk动态代理
    /*IndexDao proxy = (IndexDao) Proxy.newProxyInstance(Test.class.getClassLoader(),
            new Class[]{IndexDao.class},new ProxyInvocationHandler(new IndexDaoImpl()));

    proxy.queryString("jdkProxy",2);*/
}

}

这是一个手写的简易动态代理模型,spring底层的动态代理比这个复杂很多,不过对于一个菜鸟,对我理解代理有很大帮助。

你可能感兴趣的:(java学习,spring)