takizawajin
2010-08-07 23:21
浏览 532
已采纳

急救: java.lang.ClassCastException: $Proxy2 cannot be cast to XXXX 错误

我是个新手,这几天写了个小东西,刚开头就磕磕碰碰的,求哥哥大侠们能帮帮我.
struts1.3+spring2.0+hibernate3

报错信息:
[code="java"]
[com.jin.bbs.common.ActionExceptionHandler]-[ERROR] java.lang.ClassCastException: $Proxy2 cannot be cast to com.jin.bbs.user.UserManagerImpl
at com.jin.bbs.user.presentation.UserAction.add(UserAction.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
[/code]

applicationContext.xml
[code="xml"]
<!-- 以下是: 处理Blob类型的特殊声明 //-->

<bean id="nativeJdbcExtractor"
    class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
    lazy-init="true" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 为处理lob类型字段的句柄声明 //-->
    <!-- oracle -->
    <!-- <property name="lobHandler" ref="oracleLobHandler" /> -->
    <!-- mysql -->
    <property name="lobHandler" ref="defaultLobHandler" />
    <property name="hibernateProperties">
        <props>
            <!-- oracle --> 
            <!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
            <!-- mysql -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="jdbc.fetch_size">50</prop>
            <prop key="jdbc.batch_size">25</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/jin/bbs/user/model/UserType.hbm.xml</value>
            <value>com/jin/bbs/user/model/User.hbm.xml</value>
        </list>
    </property>
</bean>

<!-- DAO 的IoC配置//-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 事务处理的AOP配置 //-->
<bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="create*">PROPAGATION_REQUIRED</prop>
            <prop key="add*">PROPAGATION_REQUIRED</prop>
            <prop key="write*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
</bean>

<bean id="baseDaoHibernate" class="com.jin.bbs.common.BaseDaoHibernate"
    autowire="byName" />

<!-- 用户 -->
<bean id="userDao" class="com.jin.bbs.user.UserDaoHibernate" autowire="byName" />
<bean id="userManager" parent="txProxyTemplate">
    <property name="target">
        <bean class="com.jin.bbs.user.UserManagerImpl">
            <property name="userDao" ref="userDao" />
        </bean>
    </property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

[/code]

BaseAction.java
[code="java"]
public class BaseAction extends DispatchAction {
protected final Log log = LogFactory.getLog(getClass());

private static final Long defaultLong = null;

    ...................
    ...................
public Object getBean(String name) {
    ApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servlet.getServletContext());
    return ctx.getBean(name);
}

    ...................
    ...................

[/code]

UserAction.java 中的方法
[code="java"]
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("Entering 'add' method");
    }


    UserManagerImpl userManagerImpl = (UserManagerImpl) getBean("userManager");
    UserForm userForm = (UserForm)form;
    User user = (User)convert(userForm);
    try {
        user.setScore(new Integer(0));
        userManagerImpl.saveUser(user);
    } catch (Exception e) {
        log.error(e);
        return mapping.findForward("error");
    }

    return mapping.findForward("save");
}

[/code]

为什么总是会报那个错,找了半天没找到什么问题,我也用instanceof判断过getBean("userManager") 出不是UserManagerImpl 对象,这是为什么?
有没有那个大哥能帮我解决下这个问题,都纠结了一个下午了,总感觉是配置文件里的问题.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_42525443 2010-08-07 23:50
    已采纳

    这行有问题,
    UserManagerImpl userManagerImpl = (UserManagerImpl) getBean("userManager");

    Spring只能对接口AOP,你需要写一个UserManager接口,里面包含saveUser函数,然后用UserManagerImpl实现这个接口。

    代码改成
    [code="java"]
    //UserManager.java
    public interface UserManager{
    public void saveUser(User u);
    }
    //UserManagerImpl.java
    public class UserManagerImpl extends ... implements UserManager {
    public void saveUser(User u){
    ...
    }
    }

    //UserAction.java
    public ActionForward add(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

        if (log.isDebugEnabled()) {
            log.debug("Entering 'add' method");
        }
    
    
        UserManagerl userManager = (UserManager) getBean("userManager");
        UserForm userForm = (UserForm)form;
        User user = (User)convert(userForm);
        try {
            user.setScore(new Integer(0));
            userManager.saveUser(user);
        } catch (Exception e) {
            log.error(e);
            return mapping.findForward("error");
        }
    
        return mapping.findForward("save");
    }
    

    [/code]

    点赞 打赏 评论
  • works001 2010-08-07 23:54

    因此每个BO或DAO类必须要有对应的Interface,可以使用MyEclipse的重构功能生成BO或DAO类的接口定义,将获取的BO或DAO Bean放在相应接口对象的引用中即可

    常见错误 介绍 看完你肯定解决了http://www.ngiv.net/ngivHtml/program/jsp/2010/0416/586.html

    点赞 打赏 评论

相关推荐 更多相似问题