gpt4 book ai didi

java - 创建名称为 'Individual_Controller' : Injection of autowired dependencies failed; 的 bean 时出错

转载 作者:行者123 更新时间:2023-12-01 09:43:27 25 4
gpt4 key购买 nike

由于此错误,我的 tomcat 拒绝启动我的应用程序

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)}

这是我的服务等级

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);

这是我的 Controller 类,它正在调用服务层

@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);
}

这是完整的 Controller 类

@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);
}

individual_service的完整服务接口(interface)类

包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);
}

请问哪里出了问题?

最佳答案

看起来应用程序上下文中没有加载Individual_Service 类的bean。造成这种情况的原因可能有多种。

  1. 如果您使用基于 xml 的配置,请确保使用 ContextLoaderListner 将 xml 文件包含在 web.xml 中。

<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>

或者在dispatcher-servlet.xml/applicationContext.xml中

<import resource="classpath:bean.xml" />
  • 如果您使用基于注释的方法,请确保使用 @Component 或 @Bean 或 @Service 或任何其他基于应用程序要求的注释正确注释该类。
  • 确保context:component-scan中的路径覆盖Individual_Service.java包

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

    希望其中一点能够解决您的问题。如果没有,请提供您的 web.xml 、dispatcher-servlet.xml 或 Spring 配置类和 individual_Service.java 。

    关于java - 创建名称为 'Individual_Controller' : Injection of autowired dependencies failed; 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266141/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com