【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)

简介: 【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)

一、背景描述

微服务项目,spring boot (v2.1.5.RELEASE) ,今天在ServiceA微服务里添加一个功能,通过FeignClient调用ServiceB的接口。

在本项目里通过@Autowired自动注入方式注入客户端接口

@Autowired
private ScreenSaverClient screenSaverClient;

然后启动项目,结果就报 APPLICATION FAILED TO START 项目启动失败。

二、报错信息

报错内容如下图所示:

三、错误原因

报这个错,是因为启动类里的注解 @EnableFeignClients没有扫描到这个包,在本项目里是没有扫描到com.iot.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient这个。

以下是本项目的错误的启动类代码:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft.back.operation.facade.feign"})
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(OperationApplication.class, args);
  }
}

四、解决方案

方案有多种,这里先写两种,以后再更新:

4.1 方案一:扩大注解 @EnableFeignClients扫描的包的范围

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft"})
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(OperationApplication.class, args);
  }
}

网上有说“将 FeignClient 这个 bean 放在和 Application 启动类同级目录”,其道理和作用是和方案一相同的。

4.2 方案二:在注解@EnableFeignClients指定clients

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft.back.operation.facade.feign"}, clients = ScreenSaverClient.class)
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(OperationApplication.class, args);
  }
}

当然方案二有个弊端,就是项目里存在多个clients(需要调用很多其他的服务时)的时候,就需要指定很多个,所以如果通过FeignClient调用多个服务的时候,用方案一比较合适。

 

 

完结!

 

以下不用看,只是方便被搜索到

***************************

APPLICATION FAILED TO START

***************************

Description:

Parameter 0 of constructor in com.uiotsoft.back.operation.facade.service.impl.ScreenSaverClientServiceImpl required a bean of type 'com.uiotsoft.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient' that could not be found.

The injection point has the following annotations:

   - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.uiotsoft.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient' in your configuration.


相关文章
|
4月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
5月前
|
人工智能 Java API
构建基于Java的AI智能体:使用LangChain4j与Spring AI实现RAG应用
当大模型需要处理私有、实时的数据时,检索增强生成(RAG)技术成为了核心解决方案。本文深入探讨如何在Java生态中构建具备RAG能力的AI智能体。我们将介绍新兴的Spring AI项目与成熟的LangChain4j框架,详细演示如何从零开始构建一个能够查询私有知识库的智能问答系统。内容涵盖文档加载与分块、向量数据库集成、语义检索以及与大模型的最终合成,并提供完整的代码实现,为Java开发者开启构建复杂AI智能体的大门。
2754 58
|
4月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
281 8
|
5月前
|
监控 Java 数据库
从零学 Dropwizard:手把手搭轻量 Java 微服务,告别 Spring 臃肿
Dropwizard 整合 Jetty、Jersey 等成熟组件,开箱即用,无需复杂配置。轻量高效,启动快,资源占用少,内置监控、健康检查与安全防护,搭配 Docker 部署便捷,是构建生产级 Java 微服务的极简利器。
484 3
|
6月前
|
前端开发 Java 开发者
Java新手指南:在Spring MVC中使用查询字符串与参数
通过结合实际的需求和业务逻辑,开发者可以灵活地利用这些机制,为用户提供更丰富而高效的Web应用体验。
211 15
|
7月前
|
JSON 前端开发 Java
Java新手指南:如何在Spring MVC中处理请求参数
处理Spring MVC中的请求参数是通过控制器方法中的注解来完成的。这些注解包括 `@RequestParam`, `@PathVariable`, `@ModelAttribute`, `@RequestBody`, `@RequestHeader`, `@Valid`, 和 `@RequestMapping`。使用这些注解可以轻松从HTTP请求中提取所需信息,例如URL参数、表单数据或者JSON请求体,并将其转换成Java对象以供进一步处理。
573 17
|
7月前
|
安全 Java 微服务
Java 最新技术和框架实操:涵盖 JDK 21 新特性与 Spring Security 6.x 安全框架搭建
本文系统整理了Java最新技术与主流框架实操内容,涵盖Java 17+新特性(如模式匹配、文本块、记录类)、Spring Boot 3微服务开发、响应式编程(WebFlux)、容器化部署(Docker+K8s)、测试与CI/CD实践,附完整代码示例和学习资源推荐,助你构建现代Java全栈开发能力。
817 1
|
8月前
|
IDE Java 数据库连接
解决Java环境中无法识别org.mybatis.spring.annotation.MapperScan的问题。
祝你好运,在这场MyBatis的魔法冒险中获得胜利!记住,魔法书(官方文档)永远是你最好的朋友。
726 18
|
6月前
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
1254 0