I have a (spring) service sending emails via different client adapters, depending on the environment they run on. So when i am on dev
i want to use ClientAdapterA
and on live ClientAdapterB
. The usage looks like the following:
@Service
public class EmailService {
@Autowired
private ClientAdapter clientAdapter;
public EmailResponse send(EmailRequest emailRequest) {
return clientAdapter.sendMail(emailRequest);
}
}
The configuration i want to happen via my application.yml:
mail:
adapter: ClientAdapterA
I tried to use @Qualifier
but that only allows use of one hard-coded adapter:
@Qualifier("ClientAdapterB")
@AutoWired
private ClientAdapter clientAdapter;
I also tried creating a Config class which should provide the respective bean, but that didn't work either:
@Configuration
public class JavaBeansAdapterConfig {
@Value("${mail.adapter}")
private String adapterName;
@Bean
public ClientAdapter clientAdapterImplementation() throws ClassNotFoundException {
ApplicationContext ctx = new AnnotationConfigApplicationContext();
return (ClientAdapter)ctx.getAutowireCapableBeanFactory().createBean(Class.forName(adapterName));
}
}
What am i doing wrong, or is there even a way do do it like i want to? Any help is appreciated, thank you.