【问题标题】:NoSuchBeanDefinitionException: No qualifying bean of type "XInterceptor"NoSuchBeanDefinitionException:没有“XInterceptor”类型的合格 bean
【发布时间】:2020-06-18 15:41:17
【问题描述】:

我迷失了确保所有内容都明显正确注释。当我运行使用此新代码的服务时,我收到以下错误。拦截器不是已经带有@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 类,它使用我已更改的拦截器:

@Component 
@RequiredArgsConstructor 
public class someDecorator { 

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

现在是xInterceptor,它使用另一个类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 是一个配置类和一个消费者映射。

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

【问题讨论】:

    标签: java spring spring-mvc exception javabeans


    【解决方案1】:

    由于我将这些文件放在不同的包中,因此我必须添加 @ComponentScan 以及 Intercepter+Provider 所在的包名和配置文件所在的包。

    【讨论】:

      【解决方案2】:

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

      尝试将此添加到您的 ConsumerConfig 类中。

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

      【讨论】:

      • 试过了,但我需要在其中包含一个提供程序,然后为 ZProperties 引发 NoSuchBeanDefinitionException。我为它的配置文件创建了一个 bean。但仍然得到相同的异常
      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 2023-03-22
      • 2017-04-05
      • 2023-02-09
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多