我是新手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();
}
- 我認爲錯誤的..但我不能修復它。
@NguyenHey在'LocalSessionFactoryBean'配置中刪除'configurationClass'財產和嘗試。 –
我嘗試它,但有錯誤:創建名爲'studentServiceImpl'的bean時出錯:注入自動裝配依賴項失敗; –
@NguyenHuy你可以發佈堆棧跟蹤,這次錯誤可能是由於不同的屬性? –