创建名为'studentController'的bean时出错:注入自动连接的依赖项失败

时间:2016-02-21 06:38:07

标签: java spring spring-mvc

我是新手春天,我有错误。 这是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>

- 这是错误:

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

- 这是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();
}

- 这是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();
    }

- 我认为错误在..但我无法修复它。

0 个答案:

没有答案