【问题标题】:How to solve org.springframework.beans.factory.BeanNotOfRequiredTypeException error in Spring MVC如何解决 Spring MVC 中的 org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误
【发布时间】:2020-08-09 03:05:42
【问题描述】:

我是春天的新手。我尝试了一个带有 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?
  • 我的spring-webmvc版本是5.2.2.RELEASE

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


【解决方案1】:

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

顺便说一句,您应该更喜欢通过接口编码。

但是您可以使用&lt;tx:annotation-driven proxy-target-class="true"/&gt; 来指示 Spring 使用 CGLIB 代理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2023-04-02
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2021-10-12
    • 2022-07-17
    相关资源
    最近更新 更多