0

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?

Blaze
  • 2,269
  • 11
  • 40
  • 82
  • In order to make easier for people to help you, you should add your Spring context config file or class as well as the full code for `Individual_Service` and `Individual_Controller` with all annotations used. – Matt Jul 08 '16 at 12:19
  • Also tell us which version of Spring you are using, please. – Matt Jul 08 '16 at 12:20
  • Those are the complete source codes I have for my Individual_Service and Individual_Controller classes. – Blaze Jul 08 '16 at 12:22
  • My spring version is 4.2.6.RELEASE – Blaze Jul 08 '16 at 12:23
  • No, it's not complete the Spring annotations are missing, and the Spring configuration is missing as well. – Matt Jul 08 '16 at 12:24
  • please can you show me with an answer – Blaze Jul 08 '16 at 12:26
  • your `Individual_Service` class doesn't start with method declaration, it starts with package declaration then imports then class level annotations then class name with extends ... implements etc, and then only fields/methods declaration. What I'm asking is for you to provide use with the complete class starting with packgage declaration, and what's most interesting is how your class is annotated (you can skip the imports though). and also your Spring configuration (which is an xml file or a class annotated with `@Configuration`) – Matt Jul 08 '16 at 12:30
  • I have added the complete controller I am doing that for the service as well – Blaze Jul 08 '16 at 12:32
  • I have added for the Individual_Service as well – Blaze Jul 08 '16 at 12:35
  • Good, now, only the most important is missing, your Spring configuration file (xml or java class) – Matt Jul 08 '16 at 12:40
  • do you mean web.xml? – Blaze Jul 08 '16 at 12:42
  • Have a look at my answer, at the bottom, it explains where to find your Spring config. – Matt Jul 08 '16 at 12:54

2 Answers2

1

Looks like no bean of class Individual_Service is loaded in Application Context. There can be multiple reasons for that.

  1. If you are using xml based configuration , please make sure that xml file is either included in web.xml using ContextLoaderListner.

<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

<import resource="classpath:bean.xml" />
  1. If you are using annotation based Approach , make sure the class is properly annotated with @Component or @Bean or @Service or any other annotation based on application requirement.

Make sure path in context:component-scan is covering the package of Individual_Service.java

<context:component-scan base-package="net.service.datastore.*" />

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 .

amit2091
  • 346
  • 1
  • 12
  • please check my edits. I am using annotation based configuration – Blaze Jul 08 '16 at 12:38
  • Hi Blaze , Did you add any annotation on the class implementing Individual_Service interface ? It should be annotated with Component or Service or any other , based on your requirement. – amit2091 Jul 08 '16 at 12:43
  • Annotate it with Component , Service or Repository ...you can use this post to check which suits your requirement .http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in – amit2091 Jul 08 '16 at 12:52
  • Use Qualifier if there are multiple Implementation. Hope this will resolve your issue. – amit2091 Jul 08 '16 at 12:54
  • Thanks for debugging with me – Blaze Jul 08 '16 at 13:13
0

There's some information missing to provide a complete answer.

But basically, you should make sure of the following:

  1. Your IndividualService implementation class should be annotated with @Service
  2. Your IndividualController class should be annotated with @Controller
  3. The field IndividualService in IndividualController should be annotated with @Autowired (or @Inject)
  4. Both classes should be scanned in your Spring context config class (or file)

Here is how it should look like :

IndividualService.java :

package com.company.myapp.service;
//...imports...
@Service
public class IndividualService {
    //.. fields/methods...
}

IndividualController.java :

package com.company.myapp.controller;
//...imports...
@Controller
public class IndividualController{
    @Autowired
    private IndividualService individualService;
    //.. other fields/methods...
}

MyAppConfiguration.java :

package com.company.myapp;
//...imports...
@Configuration
@EnableWebMvc
//...other Spring config annotation...
@ComponentScan(basePackages = "com.company.myapp")
public class MyAppSpringConfiguration{
   //...other configuration...
}

To know which class is your Spring config class, you should have a look at your webapp descriptor file (web.xml), and see which contextConfigLocation is provided as param to the Spring servlet (DispatcherServlet). If your're user servlet 3+ without web.xml, look for a class that implements WebApplicationInitializer.

Matt
  • 3,422
  • 1
  • 23
  • 28