如何解决 Spring MVC 中的 org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误
How to solve org.springframework.beans.factory.BeanNotOfRequiredTypeException error in Spring MVC
我是 Spring 的新人。我尝试了一个使用 Spring MVC、Hibernate、Spring-tx 和 Spring ORM 的演示项目。我在那里面临错误。我的场景是:
public interface DaoInterface{
public SessionFactory getSessionFactory();
public Session getSession();
}
另一个class 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);
}
另一个class UserDao:
@Component
public class UserDao extends BaseDao{
@Override
@Transactional
public User retriveUser(String email) {
System.out.println("In userDao : retriveUser");
//other code
}
}
现在从我的服务 class 中,当我要使用 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'
我的服务class代码:
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 class 引用而不是 DaoInterface 引用来获取 UserDao 对象?
这个平台上有一些答案,但我没有得到确切的答案。我尝试过但无法解决的几个解决方案。
Spring 默认使用 JDK 动态代理($Proxy94),只能代理接口。这基本上就是为什么您可以将 userDao bean 转换为 Dao 接口,但不能转换为 BaseDao class.
顺便说一句,您应该更喜欢按接口编码。
但是您可以使用 <tx:annotation-driven proxy-target-class="true"/>
指示 Spring 使用 CGLIB 代理。
我是 Spring 的新人。我尝试了一个使用 Spring MVC、Hibernate、Spring-tx 和 Spring ORM 的演示项目。我在那里面临错误。我的场景是:
public interface DaoInterface{
public SessionFactory getSessionFactory();
public Session getSession();
}
另一个class 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);
}
另一个class UserDao:
@Component
public class UserDao extends BaseDao{
@Override
@Transactional
public User retriveUser(String email) {
System.out.println("In userDao : retriveUser");
//other code
}
}
现在从我的服务 class 中,当我要使用 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'
我的服务class代码:
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 class 引用而不是 DaoInterface 引用来获取 UserDao 对象?
这个平台上有一些答案,但我没有得到确切的答案。我尝试过但无法解决的几个解决方案。
Spring 默认使用 JDK 动态代理($Proxy94),只能代理接口。这基本上就是为什么您可以将 userDao bean 转换为 Dao 接口,但不能转换为 BaseDao class.
顺便说一句,您应该更喜欢按接口编码。
但是您可以使用 <tx:annotation-driven proxy-target-class="true"/>
指示 Spring 使用 CGLIB 代理。