我们用spring注入了一个类,
     <bean id="frameService" class="com.test.service.FrameService"  lazy-init="true">
       </bean>而宁外的一个服务类(不能有延迟注入的类)中需要这个对象,
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
WebServiceServerImpl frameServiceImpl = null;我通过这样的方式获取bean,但是我们spring的配置文件中,上面有事务的切入:
<!-- Spring AOP config -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="newServicesPointcut"
expression="execution(* com.test.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="newServicesPointcut" />
</aop:config>所以通过获取的bean又会有类型转换异常! 

解决方案 »

  1.   

    Service 类 干嘛还要延迟加载
      

  2.   

    ctx.getBean(“frameService”, FrameService.class);
      

  3.   

    这样使用会报:
    ClassCastException: Cannot cast $Proxy41
      

  4.   

    WebServiceServerImpl frameServiceImpl = null;
    你是用WebServiceServerImpl 这个的引用去获得这个bean的么, WebServiceServerImpl 和FrameService是什么关系啊.
      

  5.   


    我代码没有粘全。全文如下:
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    FrameService frameService = ctx.getBean(“frameService”, FrameService.class);
      

  6.   

    FrameService 这个类有接口吗?? 
      

  7.   


    实现了接口的! 
    我觉得问题可能在我们spring事务配置这个地方。因为我们其他的注入类,我用这样的方式是可以访问到的。除了那些有事务切入的类,我访问的时候,才会有对象转换异常!
      

  8.   

    如果她有接口,你这样写貌似就有问题了先解释一下spring AOP 实现的两种方式,一种是使用的JDK 的动态代理,是基于接口的,另一种使用的CGLIB方式,也就是基于类的代理.spring默认为那些有接口的类,使用基于接口的方式,当你在获得经过AOP处理的类的时候,这个类其实是这个接口的子类,所以不能强制转换成兄弟类.你试一下. 希望我说的没错. 我也没去验证,前段时间研读了一下spring AOP,他是这样说的