简体   繁体   中英

Error creating bean with name 'studentController': Injection of autowired dependencies failed

i am newbie Spring and i have error. This is spring-servlet.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: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>

- This is error:

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?

- This is interface 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();
    }

This is 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();
    }

- This is Interface 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();
}

- This is Class 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();
    }

- I think error in ..But i can't fix it.

First of all reading all the errors will give you the way to debug your application. They are expressive enough :

  • configurationClass is not writable. You can remove it from your spring webapplication context file

  • Could not autowire field: private com.huyliver.service.StudentService com.huyliver.controller.StudentController.studentService; and others

  • Could not autowire field: private com.huyliver.dao.StudentDAO com.huyliver.service.impl.StudentServiceImpl.studentDAO;

@Autowired fields fail because your @Autowired fields are not initialized. Maybe because :

  • the Hibernate Session is already closed when you try to use it again, raising a LazyInitializationException, then think about the OpenSessionInViewFilter
  • and/or because you do not define a bean with the name of the properties with @Autowired annotation.
 <bean id="studentService" class="com.huyliver.services.StudentService" /> <bean id="studentDAO" class="com.huyliver.dao.StudentDAO" /> 

Have a good debugging :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM