控制器类中的异常自动装配服务接口

时间:2015-03-10 05:09:24

标签: spring spring-mvc

我尝试在控制器中自动装配服务接口但是我收到错误,请帮忙。代码如下:

异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'todoComponentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.rapidinstinct.avia.plat.service.component.TodoCS com.rapidinstinct.avia.plat.service.rest.controller.TodoComponentController.tocs; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.rapidinstinct.avia.plat.service.component.TodoCS] 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)}

控制器类

@RestController
@RequestMapping ("/todo")
public class TodoComponentController extends BaseController {
    private static Logger _LOG = LoggerFactory.getLogger(TodoComponentController.class); 

    @Autowired TodoCS tocs;

     @RequestMapping(method = RequestMethod.POST)
        public create() {
            return null;
        }
}

服务界面

@Service
public interface TodoCS extends ComponentService {

}

XML文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd"
        >

    <context:annotation-config />   

    <context:component-scan base-package="com.rapidinstinct.avia.plat.service, com.rapidinstinct.avia.plat.cbo.mapper, com.rapidinstinct.avia.plat.bo.project.mapper"/> 

</beans>

3 个答案:

答案 0 :(得分:1)

您必须注释TodoCS的实现,而不是界面本身。

@Service
public class TodoCSImpl implements TodoCS {

}

然后你就可以自动上课了。

答案 1 :(得分:0)

您需要一个实现TodoCS接口的类。然后,使用@Component注释注释实现类。

答案 2 :(得分:0)

Spring,在上下文初始化时尝试在包中搜索bean名称TodoCS:com.rapidinstinct.avia.plat.service。 component .TodoCS。

在configuartion文件中,您提到的包名为:com.rapidinstinct.avia.plat.service。尝试在基本包扫描中添加com.rapidinstinct.avia.plat.service.component.TodoCS,如下所示:

<context:component-scan base-package="com.rapidinstinct.avia.plat.service, com.rapidinstinct.avia.plat.cbo.mapper, com.rapidinstinct.avia.plat.bo.project.mapper,com.rapidinstinct.avia.plat.service.component"/>