2016-02-21 42 views
1

我是新手Spring和我有错误。 这是弹簧servlet.xml中:创建名为'studentController'的bean时出错:注入自动装配依赖项失败

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.huyliver"></context:component-scan> 

    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="/WEB-INF/jdbc.properties" /> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
     <property name="packagesToScan" value="com.huyliver.model"></property> 
    </bean> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans> 

- 这是错误:

org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'studentController': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.huyliver.service.StudentService com.huyliver.controller.StudentController.studentService; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'studentServiceImpl': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.huyliver.dao.StudentDAO com.huyliver.service.impl.StudentServiceImpl.studentDAO; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'studentDAOImpl': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private org.hibernate.SessionFactory com.huyliver.dao.Impl.StudentDAOImpl.sessionFactory; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; 
nested exception is org.springframework.beans.NotWritablePropertyException: 
    Invalid property 'configurationClass' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'configurationClass' is not writable 
    or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 

- 这是接口StudentDAO:

public interface StudentDAO { 
     public void add(Student student); 
     public void edit(Student student); 
     public void delete(int studentID); 
     public Student getStudent(int studentID); 
     public List getAllStudent(); 
    } 

这是StudentDAOImpl

@Repository 
public class StudentDAOImpl implements StudentDAO { 
    @Autowired 
    private SessionFactory sessionFactory; 

    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

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

    @Override 
    public void add(Student student) { 
     sessionFactory.getCurrentSession().save(student); 
    } 

    @Override 
    public void edit(Student student) { 
     sessionFactory.getCurrentSession().update(student); 

    } 

    @Override 
    public void delete(int studentID) { 
     sessionFactory.getCurrentSession().delete(getStudent(studentID)); 

    } 

    @Override 
    public Student getStudent(int studentID) { 
     // TODO Auto-generated method stub 
     return (Student)sessionFactory.getCurrentSession().get(Student.class, studentID); 
    } 

    @Override 
    public List getAllStudent() { 
     // TODO Auto-generated method stub 
     return sessionFactory.getCurrentSession().createCriteria("from Student").list(); 
    } 

- 这是界面StudentService:

public interface StudentService { 
    public void add(Student student); 
    public void edit(Student student); 
    public void delete(int studentID); 
    public Student getStudent(int studentID); 
    public List getAllStudent(); 
} 

- 这是类StudentServiceImpl

@Service 
public class StudentServiceImpl implements StudentService { 

    @Autowired 
    private StudentDAO studentDAO; 

    @Transactional 
    public void add(Student student) { 
     studentDAO.add(student); 
    } 

    @Transactional 
    public void edit(Student student) { 
     studentDAO.edit(student); 
    } 

    @Transactional 
    public void delete(int studentID) { 
     studentDAO.delete(studentID); 

    } 

    @Transactional 
    public Student getStudent(int studentID) { 
     // TODO Auto-generated method stub 
     return studentDAO.getStudent(studentID); 
    } 

    @Transactional 
    public List getAllStudent() { 
     // TODO Auto-generated method stub 
     return studentDAO.getAllStudent(); 
    } 

- 我认为错误的..但我不能修复它。

+0

@NguyenHey在'LocalSessionFactoryBean'配置中删除'configurationClass'财产和尝试。 –

+0

我尝试它,但有错误:创建名为'studentServiceImpl'的bean时出错:注入自动装配依赖项失败; –

+0

@NguyenHuy你可以发布堆栈跟踪,这次错误可能是由于不同的属性? –

回答

0

首先读取所有错误将为您提供调试应用程序的方法。它们足够传神:

  • configurationClass不可写。你可以从你的spring web应用上下文文件中删除它

  • 无法自动装入字段:private com.huyliver.service.StudentService com.huyliver.controller.StudentController.studentService;和其他人

  • 无法自动装载字段:private com.huyliver.dao.StudentDAO com.huyliver.service.impl.StudentServiceImpl.studentDAO;

@Autowired字段失败,因为您的@Autowired字段未初始化。 也许是因为:

  • 当您试图再次使用Hibernate的Session已经关闭,提高LazyInitializationException中,再想想的OpenSessionInViewFilter
  • 和/或因为你不使用的名称来定义一个bean具有@Autowired注释的属性。
<bean id="studentService" class="com.huyliver.services.StudentService" /> 

<bean id="studentDAO" class="com.huyliver.dao.StudentDAO" /> 

有一个很好的调试:)

+0

感谢ThierryB帮助我..我试图解决它..但仍然显示错误:( –

+0

完全相同的错误? – ThierryB

相关问题