2013-08-02 112 views
-1

我會感到困惑。我試圖讓Hibernate和Spring一起工作,我無法擺脫BeanCreationException。請幫幫我。我是Spring的新手(也是Hibernate)。Spring框架 - 休眠

問題在控制器中產生,具有用@Autowired註釋的私有屬性userService。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.aerts.service.UserService com.aerts.controller.TestController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aerts.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

我真的很困惑,請幫助我一個人。

這裏是我的根context.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:jdbc="http://www.springframework.org/schema/jdbc" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 



<tx:annotation-driven /> 

<context:component-scan base-package="com.aerts.controller"> 
</context:component-scan> 


<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="classpath:application.properties" /> 


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://....."/> 
    <property name="username" value="....."/> 
    <property name="password" value="....."/> 
    <property name="initialSize" value="5"/> 
    <property name="maxActive" value="20"/> 
    </bean> 


<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

這是我User.java

package com.aerts.domain; 

import java.util.Date; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="User") 
public class User { 

    @Id 
    @GeneratedValue() 
    @Column(name="id") 
    int id; 

    @Column(name="gender") 
    private Gender gender; 

    @Column(name="birthdate") 
    private Date birthdate; 

    @Column(name="firstname") 
    private String firstname; 

    @Column(name="surname") 
    private String surname; 

    public Gender getGender() { 
     return gender; 
    } 
    public void setGender(Gender gender) { 
     this.gender = gender; 
    } 
    public Date getBirthdate() { 
     return birthdate; 
    } 
    public void setBirthdate(Date age) { 
     this.birthdate = age; 
    } 
    public String getFirstname() { 
     return firstname; 
    } 
    public void setFirstname(String firstname) { 
     this.firstname = firstname.trim(); 
    } 
    public String getSurname() { 
     return surname; 
    } 
    public void setSurname(String surname) { 
     this.surname = surname.trim(); 
    } 
} 

我在UserDAOImpl:

package com.aerts.dao; 

import java.util.ArrayList; 
import java.util.List; 

import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.stereotype.Service; 

import com.aerts.domain.User; 


@Service 
public class UserDaoImpl implements UserDao{ 

    @Autowired 
    private SessionFactory sessionFactory; 


    @Override 
    public void addUser(User user) { 
     sessionFactory.getCurrentSession().save(user);  
    } 

    @Override 
    public List<User> listUser() { 
     return sessionFactory.getCurrentSession().createQuery("from User") 
       .list(); 
    } 

    @Override 
    public void removeUser(int id) { 
     User user = (User) sessionFactory.getCurrentSession().get(
       User.class, id); 
     if (user != null) { 
      sessionFactory.getCurrentSession().delete(user); 
     }  
    } 

    @Override 
    public void updateUser(User user) { 
     sessionFactory.getCurrentSession().update(user); 

    } 

} 

而且我UserServiceImpl :

我真的很感激,如果有人可以幫助我,我會不顧一切......

+0

我相信你只是在掃描'controller'包,你的服務類在那個包之外。 – madth3

回答

1

服務類包添加到component-scan應用程序上下文文件中的基本包列表

<context:component-scan 
    base-package=" 
     com.aerts.controller 
     com.aerts.service"> 
</context:component-scan> 
+0

非常感謝你Reimeus !!!!你讓我如此開心,你無法相信!如果我可以給你投票,我做到了! :d – user2088127