My tomcat is refusing to launch my application due to this error
Error creating bean with na
me 'Individual_Controller': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Cou
ld not autowire field: private net.service.datastore.Indiv
idual_Service net.controller.Individual_Controller.S
ervice; nested exception is org.springframework.beans.factory.NoSuchBeanDefiniti
onException: No qualifying bean of type
[net.service.datastore.Individual_Service] found for dependency: expected at least 1 bean which qu
alifies as autowire candidate for this dependency. Dependency annotations: {@org
.springframework.beans.factory.annotation.Autowired(required=true)}
expected at least 1 bean which qualifies a
s autowire candidate for this dependency. Dependency annotations: {@org.springfr
amework.beans.factory.annotation.Autowired(required=true)}
this is my service class
public long createT(Individual individual);
public Individual updateT(Individual individual);
public void deleteT(String tin);
public List<Individual> getAllTs();
public Individual getT(String t);
public List<Individual> getAllTs(String individual);
this is my controller class that is calling the service layer
@Autowired
private Individual_Service service;
@RequestMapping("searchT")
public ModelAndView searchT(@RequestParam("searchName") String searchName) {
logger.info("Searching the T: "+searchName);
List<Individual> tList = service.getAllTs(searchName);
return new ModelAndView("serviceDescription", "tList", tList);
}
this is the complete controller class
@Controller
public class IndividualController {
private static final Logger logger = Logger.getLogger(IndividualController.class);
public IndividualController() {
System.out.println("Individual_Controller()");
}
@Autowired
private IndividualService service;
@RequestMapping("searchT")
public ModelAndView searchT(@RequestParam("searchName") String searchName) {
logger.info("Searching the T: "+searchName);
List<Individual> tinList = service.getAllTs(searchName);
return new ModelAndView("serviceDescription", "tList", tList);
}
complete service interface class for the individual_service
package net.service.datastore;
import java.util.List;
import net.model.Individual;
public interface IndividualService {
public long createT(Individual individual);
public Individual updateT(Individual individual);
public void deleteT(String t);
public List<Individual> getAllTs();
public Individual getT(String t);
public List<Individual> getAllTs(String individual);
}
Please what could be wrong?
There's some information missing to provide a complete answer.
But basically, you should make sure of the following:
IndividualService
implementation class should be annotated with@Service
IndividualController
class should be annotated with@Controller
IndividualService
inIndividualController
should be annotated with@Autowired
(or@Inject
)Here is how it should look like :
IndividualService.java
:IndividualController.java
:MyAppConfiguration.java
:To know which class is your Spring config class, you should have a look at your webapp descriptor file (
web.xml
), and see whichcontextConfigLocation
is provided as param to the Spring servlet (DispatcherServlet
). If your're user servlet 3+ without web.xml, look for a class that implementsWebApplicationInitializer
.Looks like no bean of class Individual_Service is loaded in Application Context. There can be multiple reasons for that.
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-bean.xml </param-value> </context-param>
or in dispatcher-servlet.xml / applicationContext.xml
Make sure path in context:component-scan is covering the package of Individual_Service.java
Hope one of these point resolve your issue. If not can you please provide your web.xml , dispatcher-servlet.xml or Spring configuration class and Individual_Service.java .