【发布时间】: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