我的控制器类:
@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();
}
}
我的道英普尔类:
@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 的 ever setter 方法中放置一个 print 语句来确认它。异常的完整堆栈跟踪如下所示:
SEVERE: Exception sending context initialized event to listener instance of class
[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException: Error 创建名为"用户控制器"的 bean:注入自动连线 依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 无法 autowire field: com.servicesapi.TechRequestService com.controllers.UsersController.techrequestservices;嵌套异常 is org.springframework.beans.factory.BeanCreationException: Error 创建名为"techRequestServiceImpl"的 bean:注入 自动连线依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 无法 autowire field: com.daoapi.TechRequestServiceDao com.servicesimpl.TechRequestServiceImpl.techrequestservicedao;嵌 套 例外情况是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No 找到 [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( 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(未知来源(
您的Component-Scan
是否正确设置为扫描给定路径内的包?尝试在spring xml configuration
中设置component-scan
以扫描相关包,如下所示:
<context:component-scan base-package="com.main"/>
您的dao
、service
和controller
包裹位于com.main
内的位置。这将扫描com.main
下的所有文件,同时检查 bean 反抗。
TechRequestServiceImpl
和TechRequestServiceDaoImpl
都实现了TechRequestService
因此上下文中有两个TechRequestService
类型的 bean,但没有类型:TechRequestServiceDao
。 解决方法:TechRequestServiceDaoImpl
应实现TechRequestServiceDao