代码片段如下
                        Object obj=SpringBeanFactory.getSpringBean(ConsumerASImpl.class);
consumerASImpl=new ConsumerASImpl();
System.out.println(obj.toString());
System.out.println(consumerASImpl.toString());
System.out.println(obj.getClass().equals(ConsumerASImpl.class));
consumerASImpl=(ConsumerASImpl) obj;---------------------------------------------------------------
打印结果如下free.flex.zone.impl.ConsumerASImpl@163d5c5
free.flex.zone.impl.ConsumerASImpl@16795f2
false
java.lang.ClassCastException: $Proxy4 cannot be cast to free.flex.zone.impl.ConsumerASImpl为神马会转换失败呢,还有 为什么是false啊

解决方案 »

  1.   

    当一个类被spring管理后,你再外部新建这个类的对象是有名无实的,它不具有任何属性  方法(就是不能用)
      

  2.   

    工厂里创建出来的bean已经不是这个class本身了,
    而是一个动态代理就像你打印出来的那样,$Proxy4 ,这是spring创建的代理类的类名了其实这很容易理解,否则spring就无法实现AOP等功能了
      

  3.   

    如果你不是有意为之,那么你检查一下是否使用了BeanNameAutoProxyCreator
      

  4.   

    spring产生的代理和consumerASImpl是一个级别的子类,所以转不了,懂么?