I'm part of the development team for a system that uses qualified injection (at runtime) of services on endpoints.
We are given the context and version and from that we create the corresponding bean.
However we use dependency lookup. We would like to use only DI.
Is there any way to do this without using calls directly from the spring container?
I researched some ways to use the container but there were always calls to the spring container.
@RestController
@RequestMapping("${spring.application.name}/{context}/v{version}")
public class SomeResource extends RestResponseResource {
@Autowired
BeanProducer producer;
@GetMapping(value = "/some-get", produces = "application/json")
public ResponseEntity<SomeResponseDTO> someResource(@PathVariable("context") String context,
@PathVariable("version") Integer version)
SomeService service = producer.getInstance(SomeServiceInterface.class, context.concat(SomeServiceInterface.class, version)).orElseThrow();
return ResponseEntity.ok(service.someMethod()).build();
}
and the producer.getInstance() method is look like
public <T> Optional<T> getInstance(Class<T> type, String qualifier, Object... args) {
Optional<T> bean = Optional.empty();
try {
bean = Optional.ofNullable(Strings.isBlank(qualifier) ?
this.beanFactory.getBean(type, args) :
this.beanFactory.getBean(qualifier, args));
} catch (BeanNotOfRequiredTypeException |
NoSuchBeanDefinitionException var6) {
log.debug("qualified bean not found", qualifier);
}
return bean;
}
As can be seen above I make use of the BeanFactory interface to obtain the desired beans from the qualifiers.
The goal is not to make use of calls to the spring IoC container classes