Issue
I'm getting lost in making sure everything is annotated properly apparently. When i run a service that uses this new code, I get the error below. Isn't the interceptor a bean already with the @Component and then everything it needs to be a bean within is a 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
I have a someDecorator
class that uses the intercepter that I have made changes to:
@Component
@RequiredArgsConstructor
public class someDecorator {
private final XInterceptor xInterceptor;
...
private void useTheInterceptor(...) {
...
aList.add(xInterceptor) // and use it for later
}
}
Now the xInterceptor
, which uses a another 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
}
The YProvider
is where it gets interesting, it has a two values. ZProperties
which is a config class and a consumer map.
@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
just captures a map from an application.yml
file:
@Configuration
@ConfigurationProperties(prefix = "some-config")
@EnableConfigurationProperties
@Getter
@Setter
public class ZProperties {
private Map<String, String> exampleMap;
}
Now to populate the consumers
map in YProvider
and to set up YProvider
, I have another config 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;
}
}
Solution
Since I had these files in different packages, I had to add @ComponentScan
with the package name of where the Intercepter+Provider were and the package of where the config files were.
Answered By - Dendin
Answer Checked By - Mary Flores (JavaFixing Volunteer)