该属性值默认为false,表示使用JDK动态代理织入增强;当值为true时,表示使用CGLib动态代理织入增强;但是,即使设置为false,如果目标类没有生命接口,

则Spring将自动使用CGLib动态代理.(以上来自:Spring3.X企业应用开发实战 P229)

 

通俗理解:

当要使用实现了某个接口的类让Spring来生成bean时,无需在aop配置中添加proxy-target-class,因为它默认为false.

但如果要使用一个指定的类,让Spring来生成bean,并使用它的某个方法时,需要在aop配置上加上一句proxy-target-class="true",否则用JUnit时,会出现:

java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to glut.daoImp2.DAOImp2

类似的错误.

  @Test  
    public void test() {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        //下面注释的两行,proxy-target-class="false"  
        //IDAO daoImp = (IDAO) ctx.getBean("DAOImp");  
        //daoImp.add();  
  
        //以下两行,proxy-target-class="true"  
        DAOImp2 daoImp2 = (DAOImp2) ctx.getBean("DAOImp2");  
        daoImp2.add2();  
    }  

 

相关文章:

  • 2021-07-03
  • 2022-01-08
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
猜你喜欢
  • 2021-11-14
  • 2022-03-07
  • 2022-12-23
  • 2021-04-27
  • 2022-12-23
  • 2022-03-10
  • 2022-12-23
相关资源
相似解决方案