接口:UserInfoService   
业务类:UserInfoServiceBean实现UserInfoService1.关于强制转换private UserInfoService userService;
private UserInfoService service;
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
userService = (UserInfoServiceBean)cxt.getBean("userInfoServiceBean"); //①
service   = (UserInfoService)cxt.getBean("userInfoServiceBean");   //②a.我使用①和②两种方法分别进行实例化,但是却发现有时会出现类型转换异常,这是为什么?
b.我将dataSource在多个bean中注入,不知是否与这个有关系?
c.使用接口强制转换与使用类强制转换有什么区别?2.关于Spring2.0
现在在学Spring 2.5,公司之前一直用自己的框架,想问下,还有必要去看Spring 2.0 吗?

解决方案 »

  1.   

    其实接口在spring 中配置中 他的class 属性 值也是他的实现类。个人觉得差不多
      

  2.   

    service   = (UserInfoService)cxt.getBean("userInfoServiceBean");   
    用这个应该是可以转化的.因为代理默认形式是用接口的.
      

  3.   

    我猜你是在配置的时候出现了问题:
    以下项目中的代码:仅供参考<bean id="interviewService" class="com.lxitedu.interview.logic.impl.InterviewServiceImpl">
         <property name="interviewDao" ref="interviewDao"></property>
    </bean>
      

  4.   

    你所说的有没有必要去研究spring 2.0,没有必要了
      

  5.   

    userService = (UserInfoServiceBean)(cxt.getBean("userInfoServiceBean"));
    你这样试试
    还不成,所正对象拿到了,反射吧