Java 如何解决SpringMVC中org.springframework.beans.factory.BeanNotOfRequiredTypeException错误

Java 如何解决SpringMVC中org.springframework.beans.factory.BeanNotOfRequiredTypeException错误,java,hibernate,spring-mvc,spring-transactions,spring-orm,Java,Hibernate,Spring Mvc,Spring Transactions,Spring Orm,春天我是新来的。我尝试了一个使用SpringMVC、Hibernate、SpringTX和SpringORM的演示项目。在那里,我面临着错误。我的设想是: public interface DaoInterface{ public SessionFactory getSessionFactory(); public Session getSession(); } 另一个类BaseDao: public abstract class BaseDao implements DaoI

春天我是新来的。我尝试了一个使用SpringMVC、Hibernate、SpringTX和SpringORM的演示项目。在那里,我面临着错误。我的设想是:

public interface DaoInterface{
    public SessionFactory getSessionFactory();
    public Session getSession();
}
另一个类BaseDao:

public abstract class BaseDao implements DaoInterface{

    @Autowired
    private SessionFactory sessionFactory;
    private Session session;

    public SessionFactory getSessionFactory() {

        return this.sessionFactory;
    }

    public Session getSession() {

        this.session = sessionFactory.getCurrentSession();
        return this.session;
    }

    public abstract User retriveUser(String email);
}
另一类UserDao:

@Component
public class UserDao extends BaseDao{

    @Override
    @Transactional
    public User retriveUser(String email) {

        System.out.println("In userDao : retriveUser");

        //other code
    }
}
现在,在我的服务类中,当我打算使用BaseDao获取UserDao对象时,我得到了错误:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'dao.BaseDao' but was actually of type 'com.sun.proxy.$Proxy94'
我的服务类别代码:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BaseDao userDao = context.getBean("userDao", BaseDao.class);
如果我执行以下操作,则不会出现错误

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DaoInterface userDao = context.getBean("userDao", DaoInterface.class);
我的applicationContext.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


<context:component-scan base-package="model" />
<context:component-scan base-package="dao" />

<mvc:annotation-driven>
     <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>

      </mvc:message-converters>
 </mvc:annotation-driven>

 <bean id="abcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo"></property>
    <property name="user" value="xxxx"></property>
    <property name="password" value="xxxxx"></property>

    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="maxIdleTime" value="30000" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="abcDataSource" />
    <property name="packagesToScan" value="model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>  
</bean>

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

<tx:annotation-driven transaction-manager="abcTransactionManager" />

</beans>

org.hibernate.dialogue.mysqldialogue
真的
所以,我的问题是,在我的应用程序中需要做些什么,以便我可以使用BaseDao类引用而不是DAO接口引用获取UserDao对象


这个平台上有一些答案,但我没有得到确切的答案。我尝试过但无法解决的解决方案很少。

Spring默认使用JDK动态代理($Proxy94),它只能代理接口。这就是为什么可以将userdaobean强制转换为Dao接口,但不能转换为BaseDao类

顺便说一下,您应该更喜欢按接口编码


但是,您可以使用
来指示Spring使用CGLIB代理。

哪个版本的Spring?My Spring webmvc版本是5.2.2.0版本