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

问题描述 投票:-1回答:2

我的控制器类:

@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,并且pojo变量正在初始化它们的值,因为我通过在pojos的setter方法中放置print语句来确认它。异常的完整堆栈跟踪如下:

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:无法自动装配字段:com.daoapi.TechRequestServiceDao com.servicesimpl.TechRequestServiceImpl.techrequestservicedao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.daoapi.TechRequestServiceDao]的限定bean:预期至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{org.springframework.beans.factory.annotation.Autowired(required = true)}位于org.springframework.beans的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)上的factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)位于org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)的org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)处的AbstractAutowireCapableBeanFactory.java:482) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java :302)在org.springframework的org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)。 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)atg.apringframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)at org.apache.catalina.core.StandardContext .listenerStart(StandardContext.java:4792)位于org.apache.catali的org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256) na.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)at java.util.concurrent.TutureTask.run(Unknown Source)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)在java.lang.Thread.run(未知来源)

spring hibernate spring-mvc spring-boot struts2
2个回答
0
投票

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


1
投票

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

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

你的daoservicecontroller包位于com.main内。这将扫描com.main下的所有文件,同时检查bean defising。

© www.soinside.com 2019 - 2024. All rights reserved.