Error creating bean with name 'Individual_Cont

2019-09-12 22:30发布

问题:

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?

回答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 .



回答2:

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.