I've got TopicGenerator interface:
public interface TopicGenerator {
File create(MultiValueMap params);
boolean accept(MultiValueMap params);
}
And 3 implementations:
@RequiredArgsConstructor
public class JavaTopicGenerator implements TopicGenerator {
//implementation ommited for readability
@RequiredArgsConstructor
public class PhpTopicGenerator implements TopicGenerator {
//implementation ommited for readability
@RequiredArgsConstructor
public class CppTopicGenerator implements TopicGenerator {
//implementation ommited for readability
Now what I try to do is use them depending on my params thats why I created special TopicFacade.
@RequiredArgsConstructor
public class TopicFacade {
@NonNull
private final TopicService topicService;
@NonNull
private final List<TopicGenerator> topicGenerators;
public void generate(MultiValueMap<String, String> params, HttpServletResponse response) {
for (TopicGenerator topicGenerator : topicGenerators) {
if (topicGenerator.accept(params)) {
File tempFile = topicService.generate(params);
//do something else.
}
}
}
}
Where on my TopicServiceImpl I've got:
@Service
@RequiredArgsConstructor
public class TopicServiceImpl implements TopicService {
@NonNull
private final List<TopicGenerator> reportGenerators;
public File generate(MultiValueMap params) {
for (TopicGenerator topicGenerator : topicGenerators) {
if (topicGenerator.create(params)) {
return topicGenerator.export(params);
}
}
I get an error like:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicServiceImpl': Unsatisfied dependency expressed through field 'topicGenerators'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<com.topic.service.TopicGenerator>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
(Earlier when I was using field injection instead of constructor I was able to add @Service
before one of the 3 implementation and code was working was working on that single implementation, but its not what I'm looking for)