java - 创建名称为 'studentController' : Injection of autowired dependencies failed 的 bean 时出错

标签 java spring spring-mvc

我是 Spring 新手,我有错误。 这是 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 接口(interface):

 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 接口(interface):

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

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

最佳答案

首先,阅读所有错误将为您提供调试应用程序的方法。他们的表现力足够强:

  • configurationClass 不可写。您可以将其从 Spring Web 应用程序上下文文件中删除

  • 无法 Autowiring 字段:private com.huyliver.service.StudentService com.huyliver.controller.StudentController.studentService;和其他

  • 无法 Autowiring 字段:private com.huyliver.dao.StudentDAO com.huyliver.service.impl.StudentServiceImpl.studentDAO;

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

  • 当您尝试再次使用 Hibernate Session 时,它已经关闭,引发 LazyInitializationException,然后考虑 OpenSessionInViewFilter
  • 和/或因为您没有使用 @Autowired 注释的属性名称定义 bean。
<bean id="studentService" class="com.huyliver.services.StudentService" />

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

祝调试顺利:)

关于java - 创建名称为 'studentController' : Injection of autowired dependencies failed 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533096/

相关文章:

java - 为什么它只取数组中的第一个元素?

java - LayerUI, g.drawImage 下划线

java - 如何回滚测试

java - 使用 Docker 和 Maven 进行 Spring Boot - JAR 中缺少 list

tomcat - 发送重定向不工作

java - 相同的语言环境在不同的机器上为相同的日期提供不同的周数

java - 如何将图像文件加载到ImageView?

java - 为什么在Spring-Hibernate Configuration中同时配置dataSource和sessionFactory?

java - Controller 中所有方法的 Spring AOP 切入点

tomcat - Spring 3.0 URL 映射问题