Inject with Spring Boot depending on properties from application.yml

In one of our common libraries for our project I need to make an distinction between two implementations of an interface based on what Service is using it.

I inject this Interface via constructor injection and need to find out how I can make sure what implementation is used based on the value of a property in our application.yml.

I looked into the @Qualifier annotation but that does seem to need an attribute in an xml schema. We don’t have such a thing.

In one part of our code we read out the properties for our KafkaListener this way

@KafkaListener(topics = "#{PathToProperties.getPrefix()}#OurBusinessProperties.getRelevantProperties()}" 

Can I use the same syntax in Spring?

In one of our common libraries for our project I need to make an
distinction between two implementations of an interface based on what
Service is using it.

You can use profiles to do this.

Let’s say you have two profiles production and test.

Then you can annotate an implementation that you want to use in production like this:

@Profile("production")
@Component
class ProductionImplementation implements MyService {
}

And on another component set a different profile:

@Profile("test")
@Component
class TestImplementation implements MyService {
}

and then start your spring-boot application with the following argument:

-Dspring.profiles.active=production

Alternatively you can select a profile using an environment variable:

SPRING_PROFILES_ACTIVE = production

One more option is to create a factory that will create a different instance depending on some environmental configuration:

@Bean
public MyService myService() {
  if (condition) return FirstImplementation();
  return SecondImplementation();
}