Search code examples
springhibernatespring-mvcspring-bootstruts2

NoSuchBeanDefinitionException raised even though @Autowire and @ repository @ service are properly configured


My controller class:

@Controller
public class UsersController
{
    @Autowired
    TechRequestService techrequestservices;

@RequestMapping(value="/service_request", method=RequestMethod.POST)
    public @ResponseBody  Map<String,Object> SaveServiceRequest(@Valid Servicerequest servicerequest,BindingResult result){
        Map<String,Object> map = new HashMap<String,Object>();

        Object obj=new Object();
            if(result.hasErrors())
            {  
                for (Object object : result.getAllErrors()) {
                    if(object instanceof FieldError) {
                        FieldError fieldError = (FieldError) object;

                     obj= (fieldError.getDefaultMessage());
                    }
                map.put("status","400");
                map.put("message",obj);
                return map;
            }}
            techrequestservices.save_servicerequest(servicerequest);
            map.put("status","200");
            map.put("message","Your record have been saved successfully"); 
            return map;
            }

}

My Service Implementation class:

@Service

    public class TechRequestServiceImpl implements TechRequestService{
        @Autowired
        TechRequestServiceDao techrequestservicedao;

        public boolean save_servicerequest(Servicerequest servicerequest) {
            return techrequestservicedao.save_servicerequest(servicerequest);
        }

        public List<Servicerequest> list() {
            // TODO Auto-generated method stub
            return techrequestservicedao.list();
        }



    }

My DaoImpl Class:

@Repository
@Transactional
public class TechRequestServiceDaoImpl implements TechRequestService {

    @Autowired
SessionFactory session;

    @Override
    public boolean save_servicerequest(Servicerequest servicerequest) {
        // TODO Auto-generated method stub
        session.getCurrentSession().saveOrUpdate(servicerequest);
        return true;
    }

    @Override
    public List<Servicerequest> list() {

            return session.getCurrentSession().createQuery("from Search_type_case").list();

    }
}

The request comes through ajax and the pojo variables are getting their values initialized properly as i confirmed it by placing a print statement in ever setter method of pojos. The full stack trace of the exception as follows:

SEVERE: Exception sending context initialized event to listener instance of class

[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.servicesapi.TechRequestService com.controllers.UsersController.techrequestservices; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'techRequestServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.daoapi.TechRequestServiceDao com.servicesimpl.TechRequestServiceImpl.techrequestservicedao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.daoapi.TechRequestServiceDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1421) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1411) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)


Solution

  • Both TechRequestServiceImpl and TechRequestServiceDaoImpl implements TechRequestService therefore there are two beans of type TechRequestService in the context, but none of type: TechRequestServiceDao. To fix: TechRequestServiceDaoImpl should implement TechRequestServiceDao