出现错误:
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&amp;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;
}

}
}

解决方案 »

  1.   

    userDaoImpl注入的类型不正确。
    感觉挺别扭UserServiceDaoImpl最好的属性类型应该是userDao而不是直接的实现,
    当你注入的时候注入userDaoImpl。
    通常都这么做。
      

  2.   

    非常感谢 closewbq,
    谢谢,我明白了:应该注入接口,而不是注入实现,否者出现代理对象不匹配类型;
    使用了接口的实现类的方法进行操作,spring是面向接口编程。
     
    再次感谢!!!