package cn.itcast.proxy;// 真实业务对象
public class ChunChun implements ChunChunInterface { @Override
public void sing(double money) {
// System.out.println("如果你能直接找到春春,演出免费");
System.out.println("春春 唱歌 唱支山歌给党听!");
} @Override
public void dance() {
System.out.println("春春 跳舞 !");
}}
接着定义了一个接口
package cn.itcast.proxy;public interface ChunChunInterface {
  //跳舞
public interface Danceable {
public void dance();//唱歌
public interface Singable {
public void sing(double money);
}}然后实现动态代理代码如下 
package cn.itcast.proxy;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;// 代理处理程序,提供代理对象
public class ChunProxy {
// 获得春春代理对象 . 返回值必须是 接口
// 根据真实 业务对象创建代理对象
public static ChunChunInterface getChunChunProxy(
final ChunChunInterface chunChun) {
// 第一个参数 被代理对象 类加载器 --- 必须先获得Class对象
// 第二个参数 被代理对象 实现接口 ---- 必须先获得 Class对象
// 第三个参数 代理对象 所有代理行为
ChunChunInterface chunChunInterface = (ChunChunInterface) Proxy
.newProxyInstance(chunChun.getClass().getClassLoader(),
chunChun.getClass().getInterfaces(),
new InvocationHandler() { // 无论程序想访问春春的哪个方法,都将执行 invoke
// 第一个参数 代理对象本身
// 第二个参数 当前代理方法
// 第三个参数 方法参数
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
System.out.println("春春代理人,拦截!");
// 唱歌 少于10w 不唱、判断当前是想让春春唱歌
if (method.getName().equals("sing")) {
// 想找春春唱歌
double money = (Double) args[0];
if (money < 100000) {
System.out.println("春春比较忙,没时间唱歌!");
} else {
System.out.println("代理人 收取费用...");
// 已经满足条件,找春春唱歌
// 真实业务对象 方法参数
return method.invoke(chunChun, args);
}
}
return null;
}
});
return chunChunInterface;
}
}
我的疑惑时这句代码  
ChunChunInterface chunChunInterface = (ChunChunInterface) Proxy
.newProxyInstance(chunChun.getClass().getClassLoader(),
chunChun.getClass().getInterfaces(),
new InvocationHandler() {}) 为什么是chunChun.getClass() 前面不是定义了一个ChunChun类吗  我感觉应该是ChunChun.class还有为什么前面是小写的chunChun啊  求指教 谢谢

解决方案 »

  1.   

    对象.getClass() 和类.class没有太大区别除了前一个空指针的错误可能性比较大。
      

  2.   

    public static ChunChunInterface getChunChunProxy(
     final ChunChunInterface chunChun) {因为方法里面已经指定了他为ChunChunInterface···
      

  3.   

    getClass和class得到的都是同一份字节码。没区别。
    只是Object没有class属性所以要用getClass获取,用定义的类名.class一样也是获取字节码
      

  4.   

    public static ChunChunInterface getChunChunProxy(
      final ChunChunInterface chunChun) {
    因为方法参数里面已经实现了ChunChunInterface接口 chunChun
      

  5.   

    使用对象.getClass()这样其实可能更灵活一些,假设这里只是生产代理对象呢,所有的代理对象都由这里生产不光只有ChunChun这个呢?那你直接ChunChun.class是否无法满足呢?所有由传入对象来觉得创建什么样类型的代理对象不是挺好的么?要不何必设置一个传入参数呢?
    举个例子:public class ObjectDynamicProxy implements InvocationHandler{ //真实对象引入
    private Object proxyObj; public void setObj(Object obj) {
    this.proxyObj = obj;
    }

    public ObjectDynamicProxy(Object obj){
    this.proxyObj = obj;
    } public Object invoke(Object object, Method method, Object[] args)
    throws Throwable {

    System.out.println("before dynamic proxy method.");

    Object obj = method.invoke(proxyObj, args);

    System.out.println("after dynamic proxy method.");

    return obj;
    }

    /**
     * 代理对象生产工厂
     * @param obj
     * @return
     */
    public static Object factory(Object obj) {
    //获取传入对象的Class对象
    Class<?> classType = obj.getClass(); //生成代理对象并返回
    //该对象实现传入对象所实现的接口
    //生成的代理对象的所有方法都由第三个参数的invoke方法来接管和调用
    return Proxy.newProxyInstance(classType.getClassLoader(), classType
    .getInterfaces(), new ObjectDynamicProxy(obj));
    }
    }
      

  6.   

    你代码里面  private Object proxyObj;       public void setObj(Object obj) {         this.proxyObj = obj;     }           public ObjectDynamicProxy(Object obj){         this.proxyObj = obj  这些this. 是啥意思呢
      

  7.   

    this.表示当前对象的。你主要看一下factory方法就好
      

  8.   

    谢谢你的解释  我知道一般是对象.getClass  类.class 或者Class.forName(类)  我想请教的是我那个被代理对象是ChunChun 然后那个方法里面为什么写小写的chunChun  那里面是chunChun.getClass也是是说chunChun是对象  但是我看代码没有写过ChunChun chunChun=new ChunChun()这句代码啊  小弟都是自学的  请指教 谢谢
      

  9.   

    我知道一般是对象.getClass  类.class 或者Class.forName(类)  我想请教的是我那个被代理对象是ChunChun 然后那个方法里面为什么写小写的chunChun  那里面是chunChun.getClass也是是说chunChun是对象  但是我看代码没有写过ChunChun chunChun=new ChunChun()这句代码啊  小弟都是自学的  请指教 谢谢 
      

  10.   

    动态代理呗。其实我也不大懂,就是以前看过的视频教程里面讲过,那时候讲AOP,我也迷糊了好久。
    至于楼主问的问题,我认为8楼已经回答了。
    附上以前我写的代码。
    package org.aop;
     
    public interface Target
    {
        void doSomething();
    }
    ///////////////
    package org.aop;
     
    public class MyTarget implements Target
    {
        @Override
        public void doSomething()
        {
        System.out.println("doSomething^^^^^^");
        }
    }
    ///////////////////
    package org.aop;
     
    public class MyInterceptor
    {
        public void doBefore()
        {
        System.out.println("before++++++++++++++++");
        }
        public void doAfter()
        {
        System.out.println("after------------------");
        }
    }
    ///////////////////////////
    package org.aop;
     
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
     
    public class MyHandler implements InvocationHandler
    {
        private Target target;
        public MyHandler(Target target)
        {
        this.target = target;
        }
        private MyInterceptor myInterceptor = new MyInterceptor();
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable
        {
        myInterceptor.doBefore();
        Object res = method.invoke(target, args);
        myInterceptor.doAfter();
        return res;
        }
    }
    ////////////////////////
    package org.aop;
     
    import java.lang.reflect.Proxy;
     
    public class MyProxy
    {
        public Target getProxy(Target obj)
        {
        Target target = new MyTarget();
        MyHandler handler = new MyHandler(target);
        return (Target) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
        }
    }
    ///////////////////////////////
    package org.aop;
     
    public class Client
    {
        public static void main(String[] args)
        {
        Target target = new MyTarget();
        MyProxy myProxy = new MyProxy();
        Target proxy = myProxy.getProxy(target);
        proxy.doSomething();
        }
    }
      

  11.   

    public static ChunChunInterface getChunChunProxy(
     final ChunChunInterface chunChun) {
    首先你方法里实例化了ChunChunInterface接口 为chunChun
    而你chunChun.getClass()是获取这个接口  然后chunChun.getClass().getInterfaces()是获取该接口的内部接口  其实跟内部类差不多  多去看看方法java的方法定义和内部类
      

  12.   

    public static ChunChunInterface getChunChunProxy(
    final ChunChunInterface chunChun) {
    这个是你的方法入口,这里传入了对象chunChun啊.....