即使正确配置了@Autowire和@存储库@服务,也会引发NoSuchBeanDefinitionException

时间:2018-09-15 18:44:46

标签: spring hibernate spring-mvc spring-boot struts2

我的控制器类:

@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;
            }

}

我的服务实现类:

@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类:

@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();

    }
}
  

通过ajax发出请求,并且通过将打印语句放入pojos的ever setter方法中来确认它,pojo变量正在正确初始化其值。异常的完整堆栈跟踪如下:

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

[org.springframework.web.context.ContextLoaderListener]   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ usersController”的bean:自动接线的注入   依赖失败;嵌套异常为   org.springframework.beans.factory.BeanCreationException:无法   自动连线栏位:com.servicesapi.TechRequestService   com.controllers.UsersController.techrequestservices;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ techRequestServiceImpl”的bean:注入   自动关联依赖项失败;嵌套异常为   org.springframework.beans.factory.BeanCreationException:无法   autowire字段:com.daoapi.TechRequestServiceDao   com.servicesimpl.TechRequestServiceImpl.techrequestservicedao;嵌套的   例外是   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   找到了类型为[com.daoapi.TechRequestServiceDao]的合格Bean   依赖关系:至少需要1个符合自动装配条件的bean   此依赖项的候选者。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(required = true)}     在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)     在   org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)     在   org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)     在   org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)     在   org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1421)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1411)     在java.util.concurrent.FutureTask.run(未知来源)   java.util.concurrent.ThreadPoolExecutor.runWorker(未知源),位于   java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)   java.lang.Thread.run(未知来源)

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

TechRequestServiceImplTechRequestServiceDaoImpl都实现TechRequestService,因此在上下文中有两个类型为TechRequestService的bean,但没有一个类型为TechRequestServiceDao。 要解决:TechRequestServiceDaoImpl应该实现TechRequestServiceDao