请高手帮忙,ssh集成开发实验,出现如下错误....

qqzy168 2010-10-20 03:21:20
出现错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceDaoImpl': Injection of resource fields failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDaoImpl' must be of type [com.bookstore.daoImp.UserDaoImpl], but was actually of type [$Proxy12]
...

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'userDaoImpl' must be of type [com.bookstore.daoImp.UserDaoImpl], but was actually of type [$Proxy12]


beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 打开spring的注解功能 和扫描功能-->
<context:component-scan base-package="com.bookstore"/>

<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="500"/>
<property name="maxIdle" value="2"/>
<property name="minIdle" value="1"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.bookstore.bean.User</value>
<value>com.bookstore.bean.BookSort</value>
<value>com.bookstore.bean.Book</value>
<value>com.bookstore.bean.BookOrder</value>
<value>com.bookstore.bean.BookOrderItem</value>
</list>
</property>

<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>

<!-- 添加spring的事务管理处理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 开启spring事务管理功能,这里推荐注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager"/>




</beans>


UserServiceDaoImpl.java如下:

package com.bookstore.serviceImpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.bookstore.bean.User;
import com.bookstore.daoImp.UserDaoImpl;
import com.bookstore.service.UserServiceDao;

@Component
public class UserServiceDaoImpl implements UserServiceDao {

@Resource
private UserDaoImpl userDaoImpl;
public UserDaoImpl getUserDaoImpl() {
return userDaoImpl;
}

public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}

public void saveUser(User user) {
userDaoImpl.saveUser(user);
}

public boolean validateUser(String userName, String passsWord) {

return userDaoImpl.validateUser(userName, passsWord);
}

}


UserDaoImpl.java如下:

package com.bookstore.daoImp;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bookstore.bean.User;
import com.bookstore.dao.UserDao;

@Component
public class UserDaoImpl implements UserDao {

@Resource
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Transactional
public void saveUser(User user) {
sessionFactory.getCurrentSession().save(user);

}

public boolean validateUser(String userName, String passsWord) {

Query q=sessionFactory.getCurrentSession().createQuery("from User where userName=? and passWord=?");
q.setString(0, userName);
q.setString(1, passsWord);
User u=(User)q.uniqueResult();
if(u!=null)
{
//true表示用户存在 或者登陆成功
return true;
}

else
{
return false;
}

}
}
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqzy168 2010-10-20
  • 打赏
  • 举报
回复
非常感谢 closewbq,
谢谢,我明白了:应该注入接口,而不是注入实现,否者出现代理对象不匹配类型;
使用了接口的实现类的方法进行操作,spring是面向接口编程。

再次感谢!!!
closewbq 2010-10-20
  • 打赏
  • 举报
回复
userDaoImpl注入的类型不正确。
感觉挺别扭UserServiceDaoImpl最好的属性类型应该是userDao而不是直接的实现,
当你注入的时候注入userDaoImpl。
通常都这么做。
qqzy168 2010-10-20
  • 打赏
  • 举报
回复
请高手们,帮忙找错误啊,谢谢大家啦!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧