![](/img/trans.png)
[英]NoSuchBeanDefinitionException: No qualifying bean of type 'int'
[英]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
類,它使用我已更改的攔截器:
@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;
}
}
這可能是因為您沒有在配置文件中將 XInterceptor 作為 Bean 提供。
嘗試將其添加到您的 ConsumerConfig 類中。
@Bean
public XInterceptor getXInterceptor(){
return new XInterceptor();
}
由於我將這些文件放在不同的包中,因此我必須添加@ComponentScan
以及 Intercepter+Provider 所在位置的包名稱和配置文件所在位置的包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.