0
    // 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]

Code for Student Controller and Student Service:

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 file

<?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>
Jens
  • 67,715
  • 15
  • 98
  • 113
Dikshant
  • 3
  • 4
  • Can you add more detail about this problem? (e.g. a stack trace or where the autowired fails) – Xstian Aug 17 '14 at 12:36
  • okay.. I am adding the details – Dikshant Aug 17 '14 at 16:58
  • @Dikshant Please post the code for `StudentController` and `StudentService` – geoand Aug 17 '14 at 17:07
  • Looks like [commons-pool](http://search.maven.org/#artifactdetails%7Ccommons-pool%7Ccommons-pool%7C1.6%7Cjar) (dependency of [DBCP](http://search.maven.org/#artifactdetails%7Ccommons-dbcp%7Ccommons-dbcp%7C1.4%7Cjar)) is missing from the classpath. Might want to consider upgrading these to version 2.x anyway, assuming you're running Java 7+. – superEb Aug 17 '14 at 20:02
  • @superEb : I added commons-pool now. its jar file is added in maven dependencies. Now it is given another error> Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing Class – Dikshant Aug 22 '14 at 17:54
  • Sounds like a binary incompatibility between library dependencies. See this: http://stackoverflow.com/questions/1980452/what-causes-java-lang-incompatibleclasschangeerror – superEb Aug 22 '14 at 19:55

0 Answers0