NoSuchBeanDefinitionException:没有 "XInterceptor" 类型的合格 bean

NoSuchBeanDefinitionException: No qualifying bean of type "XInterceptor"

我显然迷失了确保所有内容都正确注释的过程。当我 运行 使用此新代码的服务时,出现以下错误。拦截器不是已经带有 @Component 的 bean,然后它需要成为 bean 的所有内容都是 bean 吗?

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.demo...XInterceptor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    ... 88 common frames omitted

Process finished with exit code 1

我有一个 someDecorator class,它使用我已更改的拦截器:

@Component 
@RequiredArgsConstructor 
public class someDecorator { 

    private final XInterceptor xInterceptor; 
    ...
    private void useTheInterceptor(...) {
      ...
      aList.add(xInterceptor) // and use it for later
    }
}

现在 xInterceptor,它使用另一个 class YProvider

@Component
@RequiredArgsConstructor
public class xInterceptor {

    private final YProvider yProvider;

    public ClientHttpResponse intercept(String str, ...) throws IOException {

        Consumer<String> loggingConsumer = yProvider.getLoggingLevel(str);
        // ... use the consumer 
    }

YProvider 是有趣的地方,它有两个值。 ZProperties 这是一个配置 class 和一个消费者地图。

@RequiredArgsConstructor
public class YProvider {

    private final ZProperties zProperties;
    private final Map<String, Consumer<String>> consumers;

    public Consumer<String> getLoggingLevel(String str) {
       // gets a single consumer from zProperties.getExampleMap ...
}

ZProperties 刚刚从 application.yml 文件中捕获了一张地图:

@Configuration
@ConfigurationProperties(prefix = "some-config")
@EnableConfigurationProperties
@Getter
@Setter
public class ZProperties {
    private Map<String, String> exampleMap;
}

现在要在 YProvider 中填充 consumers 地图并设置 YProvider,我有另一个配置 ConsumerConfig

@Configuration
public class ConsumerConfig {

    @Bean
    public YProvider yProvider(ZProperties zProperties) {
        return new YProvider(zProperties, exmapleMapToConsumerConfiguration());
    }

    public Map<String, Consumer<String>> exmapleMapToConsumerConfiguration() {
        Map<String, Consumer<String>> exmapleMapToConsumerMap = new ConcurrentHashMap<>();
        // add stuff to map

        return exmapleMapToConsumerMap;
    }
}

可能是因为您没有在配置文件中将 XInterceptor 作为 Bean 提供。

尝试将此添加到您的 ConsumerConfig class。

    @Bean
    public XInterceptor getXInterceptor(){
     return new XInterceptor();
    }

因为我在不同的包中有这些文件,所以我不得不添加 @ComponentScan 拦截器+提供程序所在的包名称和配置文件所在的包。