// it is throwing `beanfactoryexception` and injection of autowired dependencies failed.
// the coding is done in maven eclipse
15:55:39.544 [http-bio-4137-exec-7] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed
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.dixy.service.StudentService com.dixy.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.dixy.dao.StudentDao com.dixy.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.dixy.dao.impl.StudentDaoImpl.session; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
学生管理员和学生服务代码:
package com.dixy.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.dixy.model.Student;
import com.dixy.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/index")
public String setupForm(Map<String, Object> map){
Student student = new Student();
map.put("student", student);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/student.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Student student, BindingResult result, @RequestParam String action, Map<String, Object> map)
{
Student studentResult = new Student();
switch(action.toLowerCase()){ //only in Java7 you can put String in switch
case "add":
studentService.add(student);
studentResult = student;
break;
case "edit":
studentService.edit(student);
studentResult = student;
break;
case "delete":
studentService.delete(student.getStudentId());
studentResult = new Student();
break;
case "search":
Student searchedStudent = studentService.getStudent(student.getStudentId());
studentResult = searchedStudent!=null ? searchedStudent : new Student();
break;
}
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
}
package com.dixy.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dixy.dao.StudentDao;
import com.dixy.model.Student;
import com.dixy.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Transactional
public void add(Student student) {
// TODO Auto-generated method stub
studentDao.add(student);
}
@Transactional
public void edit(Student student) {
// TODO Auto-generated method stub
studentDao.edit(student);
}
@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();
}
@Transactional
public void delete(int studentId) {
// TODO Auto-generated method stub`enter code here`
studentDao.delete(studentId);
}
}
// Dispatcher-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<context:annotation-config />
<context:component-scan base-package="com.dixy" />
<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.hibernate3.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>
</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 />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>