Reputation: 808
I and trying to autowire a service interface in the controller but i am getting an error, please help. Codes below :
Exception
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)}
Controller class
@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 Interface
@Service
public interface TodoCS extends ComponentService {
}
XML file
<?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>
Upvotes: 1
Views: 1536
Reputation: 69440
You have to annotate the implementation of TodoCS
Not the interface itself.
@Service
public class TodoCSImpl implements TodoCS {
}
Then you can autowire the class.
Upvotes: 1
Reputation: 29266
Spring, at time of context initialization is trying to search a bean name TodoCS in package : com.rapidinstinct.avia.plat.service.component.TodoCS.
And in configuartion file you mentioned package name as : com.rapidinstinct.avia.plat.service. Try adding com.rapidinstinct.avia.plat.service.component.TodoCS in base package scan as below:
<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"/>
Upvotes: 0
Reputation: 8067
You need a class which implements TodoCS
interface. Then, annotation the implementation class with @Component
annotation.
Upvotes: 0