对于静态代理我很理解了,但是动态代理还是不太懂,有人能举例说明下吗。一定要用例子,说的越详细越好。拜谢了

解决方案 »

  1.   


    package com.design.proxy;import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;public class ProxySourceTest {
    public static void main(String[] args) throws SecurityException,
    NoSuchMethodException, Throwable {
    ProxyTestInterface p = (ProxyTestInterface) MyInvocationHandler
    .newProxy(new ProxyTestImpl());
    /*
     * byte[] b =
     * ProxyGenerator.generateProxyClass("ProxyTestInterfaceProxy", new
     * Class[] { p.getClass() });
     * 
     * File file=newFile(
     * "D:\\workspace8.5\\MTSJMS\\bin\\com\\design\\proxy\\ProxyTestInterfaceProxy.class"
     * );
     * 
     * FileOutputStream fos=new FileOutputStream(file); fos.write(b);
     * fos.close();
     */
    System.out.println(p.hello("123"));
    }
    }class MyInvocationHandler implements InvocationHandler {
    private Object object; public MyInvocationHandler(Object object) {
    this.object = object;
    } public static Object newProxy(Object object) {
    return Proxy.newProxyInstance(object.getClass().getClassLoader(),
    object.getClass().getInterfaces(), new MyInvocationHandler(
    object));
    } public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {

    System.out.println(method.invoke(object, args)); return "error";
    }
    }
      

  2.   


    package com.design.proxy;import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;public class ProxySourceTest {
    public static void main(String[] args) throws SecurityException,
    NoSuchMethodException, Throwable {
    // newProxy方法就是返回你想代理的实现类的代理对象
    ProxyTestInterface p = (ProxyTestInterface) MyInvocationHandler
    .newProxy(new ProxyTestImpl());
    // 这个p.hello("123")就是代理对象调用
    System.out.println(p.hello("123"));
    }
    }class MyInvocationHandler implements InvocationHandler {
    private Object object; public MyInvocationHandler(Object object) {
    this.object = object;
    } /**
     * 
     * @param object
     *            需要代理的对象
     * @return 返回被代理对象的代理对象..有点绕口啊
     */
    public static Object newProxy(Object object) {
    return Proxy.newProxyInstance(object.getClass().getClassLoader(),
    object.getClass().getInterfaces(), new MyInvocationHandler(
    object));
    } /**
     * 这个invoke方法其实是上面p.hello()调用的.
     * 下面这个是反编译的代理对象里面的hello方法,p.hello()然后里面调用下面这个invoke方法
     *     public final String hello(String s)
        {
            try
            {
                return (String)super.h.invoke(this, m3, new Object[] {
                    s
                });
            }
     */
    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {

    //这里是反射调用真实对象的hello方法
    System.out.println(method.invoke(object, args)); //return 是我随意返回的。。-。-
    return "error";
    }
    }
      

  3.   

    spring aop使用了代理模式,当目标对象是接口时作用JDK动态代理,普通类时则使用CGLIB代理
    下面是JDK动态代理一个示例public interface UserDao {
    /**
     * 切入点
     */
    public void update(); public void delete();
    }
    public class UserDaoImpl implements UserDao { public void delete(){
    System.out.println("UserDao delete函数");
    } public void update(){
    System.out.println("UserDao update函数");

    }}public class UserInvocationHandler implements InvocationHandler {
    private Object invocationObj; public UserInvocationHandler(Object invocationObj) {
    this.invocationObj = invocationObj;
    } @Override
    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    // TODO Auto-generated method stub
    System.out.println("走JDK动态代理调用函数");// 此方法为:通知
    Object obj = method.invoke(invocationObj, args);
    return obj;
    }}/**
     * JDK 动态代理
     * 
     */
    public class App {
    @Test
    /**
     * Aspect-Oriented Programming
     */
    public void jdkProxy() {
    UserDao dao = new UserDaoImpl();// dao为:目标对象
    Object obj = Proxy.newProxyInstance(dao.getClass().getClassLoader(),
    dao.getClass().getInterfaces(), new UserInvocationHandler(dao));
    UserDao userdao = (UserDao) obj;
    // 以下两个方法为:连接点
    userdao.delete();
    userdao.update();
    }
    }
      

  4.   

    /**
         * 这个invoke方法其实是上面p.hello()调用的.
         * 下面这个是反编译的代理对象里面的hello方法,p.hello()然后里面调用下面这个invoke方法
         *     public final String hello(String s)
        {
            try
            {
                return (String)super.h.invoke(this, m3, new Object[] {
                    s
                });
            }
         */这段能再解释下吗?什么时候会调用那个HELLO()
      

  5.   

      // newProxy方法就是返回你想代理的实现类的代理对象
            ProxyTestInterface p = (ProxyTestInterface) MyInvocationHandler
                    .newProxy(new ProxyTestImpl());
            // 这个p.hello("123")就是代理对象调用
            System.out.println(p.hello("123"));
    main方法里不是.hello了嘛。。这个代码只是告诉你代理对象是如何做的。。反编译出来看的,你在main里看的是.hello 其实代理对象是hello方法里又 invoke了。然后在invoke方法里 通过反射来调用真实对象的hello方法
      

  6.   

    貌似有一些些明白了,这个模式很重要吗?因为SPRING里有用到,所以才看看的
      

  7.   

    一般永不到的 只是让你更了解spring 的 AOP
      

  8.   

    记住“InvocationHandler”的使用就可以了,AOP本身不难理解。