Spring框架学习1.0对动态代理的理解z,自定义BeanFactory

自定义一个接口 和一个接实现类

public interface Hello {
    void setInfo(String a,String b);
    String getInfo();
    void hah(String s);
}

/**
 * Created by likailong on 2016/9/29.
 */
public class HelloIml implements Hello {
    private String a;
    private String b;
    public void setInfo(String a, String b){
        this.a=a;
        this.b=b;
    }
    @Override
    public String getInfo() {
        return a;
    }

    public void hah(String s){
        System.out.print("ssss"+s);
    }
}

package cn.itcast.shujujiegou.StructuresAnalysis;

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

/**
 * Created by likailong on 2016/9/29.
 */
public class AopHandler implements InvocationHandler {
    private Object target;
    public AopHandler(Object target){
        this.target=target;
    }
    public void println(String str,Object...args){
        System.out.println(str);
        if(args==null){
            System.out.println("没有传递任何值");
        }else{
            for(Object obj:args){
                System.out.println(obj);
            }
        }
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("\n\n======调用方法名"+method.getName());
        Class<?>[] variables=method.getParameterTypes();
        System.out.println("\n\t 参数列表:\n");
        for(Class<?> typevariables:variables){
            System.out.println("\t\t\t"+typevariables.getName());
        }
        println("\n\n\t 传入参数值为:\n");
        for(Object arg:args){
            System.out.println("\t\t\t"+arg);
        }
        Object result=method.invoke(target,args);
        println("返回的参数为",result);
        println("返回的类型为",method.getReturnType());
        return result;
    }
}
动态代理的实现如上代码,这只是实现的一种。下面测试上面的代码

public class MethodInvokeSample {
    public static void main(String [] args) throws Exception {
        Method method=MethodInvokeSample.class.getDeclaredMethod("test",String.class,int.class);
        String result=(String)method.invoke(null,"fuck",21);
        System.out.println(result);
    }
    public static String test(String a,int b){
        return "传入参数:"+a+"传入参数:"+b;
    }
}

结果


动态将方法执行并且传入参数。但是上面没有展现出面向切面只是动态传入了一个值下面自定义BeanFactory

package cn.itcast.shujujiegou.StructuresAnalysis;

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

/**
 * Created by likailong on 2016/9/29.
 */
public class BeanFactory {
    public static Object getBean(String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Object obj=Class.forName(classname).newInstance();
        InvocationHandler handle = new AopHandler(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handle);
    }
    public static<T> T getBean(String classname,Class<T> clazz) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        return (T)getBean(classname);
    }
}

测试
package cn.itcast.shujujiegou.StructuresAnalysis;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by likailong on 2016/9/29.
 */
public class BeanfatoryTest {
    public static void main(String [] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Hello aa = BeanFactory.getBean("cn.itcast.shujujiegou.StructuresAnalysis.HelloIml", HelloIml.class);
       aa.setInfo("hjaslg","sldkjgs");
        Method method = Hello.class.getDeclaredMethod("setInfo", String.class, String.class);
        method.invoke(new HelloIml(),"aaa","bbb");
    }
}

分析newinstance实际原理

从图看出hello接口是代理指向因此代理对象与实现类无关用了字节码增强技术,故代理对象指向接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值