java - 如何解决 Spring MVC 中的 org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误

标签 java hibernate spring-mvc spring-transactions spring-orm

我是 Spring 的新人。我尝试过使用 Spring MVC、Hibernate、Spring-tx 和 Spring ORM 的演示项目。在那里我面临着错误。我的场景是:

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>

所以,我的问题是,我的应用程序需要做什么,以便我可以使用 BaseDao 类引用而不是 DaoInterface 引用获取 UserDao 对象?

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

最佳答案

Spring默认使用JDK动态代理($Proxy94),只能代理接口(interface)。这基本上就是为什么您可以将 userDao bean 转换为 Dao 接口(interface),但不能转换为 BaseDao 类。

顺便说一句,您应该更喜欢通过接口(interface)进行编码。

但是您可以使用<tx:annotation-driven proxy-target-class="true"/>指示 Spring 使用 CGLIB 代理。

关于java - 如何解决 Spring MVC 中的 org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427046/

相关文章:

java - 从 Map<String, Map<String, Object>> 获取不正确的值

java构造函数在实现中调用另一个被重写的构造函数

java - hibernate ,按 id 或唯一列选择

java - 父类中@Id 和基类中唯一序列的正确 JPA 映射是什么

java - 为什么 Byte [] 在 JSON View 中被转换为 String

java - 我可以在 Spring MVC 的拦截器中使用 RedirectAttributes 或 FlashMap

javascript - Spring mvc security and WEB-INF js,images access-拒绝从 'http://localhost:8081/xyz/static/internal/js/jquery-1.10.2.min.js'执行脚本

java - Java中的Hive Udf(加解密)

java - JTable 未在 java 中显示

java - Spring 可以/应该为子事务重用 Hibernate Session (REQUIRES_NEW)