I have a component EmbeddedRedis that depends on a configuration object RedisConfig parsed from the application's property file. There are different property files, corresponding to the possible application profiles that can be run. Thus, when run in profile master, the component EmbeddedRedis will be provisioned according to the master profile.
In a test class, that is supposed to set-up a local Redis cluster, I also require Redis objects provisioned according to all other profiles. I sketched my idea below using the @Qualifier annotation, which does not bring the desired result.
@Autowired @Qualifier("dev-cluster-master")
private Redis embeddedRedisMaster;
@Autowired @Qualifier("dev-cluster-slave-001")
private Redis embeddedRedisSlave1;
@Autowired @Qualifier("dev-cluster-slave-002")
private Redis embeddedRedisSlave2;
How can I archive the desired result in Spring Boot? If that doesn't work directly, would it also suffice to obtain the before-mentioned configuration objects parsed from the different property files.
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisConfig {
....
}
Thanks in advance!
You can do something like this: Consider you have a class definition (Redis in your example)
And a configuration class like:
which initiate 2 different objects based on current active profile. If current active profile is "master", then serverConfig1() will get executed, otherwise serverConfig2().
And finally autowired your service/object like this:
This will depends on above executed bean definition in configuration file.
And property file should look like this:
So in this example, after executing above code, the value of 'name' in
CustomService service;will be "slave" instead of "master", because current active profile is "slave" and thus "serverConfig2()" will get executed