即使正确配置了 @Autowire 和 @repository @service,也会引发 NoSuchBeanDefinitionException

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

我的控制器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;
            }

}

我的服务实现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();
        }



    }

我的 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.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)

您的 Component-Scan 设置是否正确以扫描给定路径内的包?尝试设置spring xml configuration中的component-scan扫描相关包如下:

<context:component-scan base-package="com.main"/> 

您的 daoservicecontroller 包位于 com.main 中。这将在检查 bean 定义时扫描 com.main 下的所有文件。

TechRequestServiceImplTechRequestServiceDaoImpl 都实现了 TechRequestService 因此上下文中有两个类型为 TechRequestService 的 bean,但是 none 类型为:TechRequestServiceDao。 修复:TechRequestServiceDaoImpl 应该实现 TechRequestServiceDao