Feign Client 在组件扫描期间被忽略

Feign Client being ignored during component scan

我正在创建一个模拟客户端来访问外部服务。我将以下依赖项添加到我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>in.njari</groupId>
    <artifactId>util</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>util</name>
    <description>Utilities for API Dev</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

这里的两个加法是spring-cloud-starter-openfeignspring-cloud-dependencies

这是我的假客户 class :

@FeignClient(name = "chat-service", url = "${server.chatservice.url}")
public interface ChatServiceClient {

    @PostMapping("/email/send")
    void sendEmail(@RequestBody Map<String, String> sendEmailMap);
}

接下来,我将在我现有的一项服务中自动装配它。但是,这会导致错误。我错过了什么?

我尝试将 @Service 添加到我的 chatServiceClient 中,但这只是给我这个警告: 2022-04-11 17:02:07.358 DEBUG 87001 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Ignored because not a concrete top-level class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/client/ChatServiceClient.class]

我认为@FeignClient 应该足以将其注册为 bean,但事实并非如此。我还需要什么?

2022-04-11 16:50:41.917  INFO 86938 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$7290de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2022-04-11 16:50:42.110  INFO 86938 --- [           main] in.njari.util.UtilApplication            : No active profile set, falling back to default profiles: default
2022-04-11 16:50:42.236 DEBUG 86938 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/ExceptionReportingAdvice.class]
2022-04-11 16:50:42.268 DEBUG 86938 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/controller/UtilController.class]
2022-04-11 16:50:42.978  INFO 86938 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=9ddeaaa5-b9f2-314c-82de-bbd43b8d74ad
2022-04-11 16:50:43.129  INFO 86938 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$7290de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-04-11 16:50:43.732  INFO 86938 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-04-11 16:50:43.753  INFO 86938 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-04-11 16:50:43.753  INFO 86938 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-04-11 16:50:43.887  INFO 86938 --- [           main] o.a.c.c.C.[.[localhost].[/util-service]  : Initializing Spring embedded WebApplicationContext
2022-04-11 16:50:43.888  INFO 86938 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1722 ms
2022-04-11 16:50:43.930  WARN 86938 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exceptionReportingAdvice': Unsatisfied dependency expressed through field 'chatServiceClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'in.njari.util.src.client.ChatServiceClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-04-11 16:50:43.932  INFO 86938 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-04-11 16:50:43.948  INFO 86938 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-11 16:50:43.948 DEBUG 86938 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@28cda624, started on Mon Apr 11 16:50:41 IST 2022
2022-04-11 16:50:44.091 ERROR 86938 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field chatServiceClient in in.njari.util.src.ExceptionReportingAdvice required a bean of type 'in.njari.util.src.client.ChatServiceClient' 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 'in.njari.util.src.client.ChatServiceClient' in your configuration.


Process finished with exit code 1

尝试在您的主 class 中添加 @EnableFeignClients:

@SpringBootApplication
@EnableFeignClients
public class ExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

查看更多:https://www.baeldung.com/spring-cloud-openfeign