上記の私のコメントによると、ここに私の提案があります:
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class DoubleProfilesCondition implements Condition {
public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
int counter = 0;
for (int i = 0; i < activeProfiles.length; i++) {
String profile = activeProfiles[i];
if (profile.equals("profile1") || profile.equals("profile2")) {
counter++;
}
}
if (counter == 2)
return true;
return false;
}
}
そして、どの Bean が作成されるかを指示するクラス:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
@Conditional(DoubleProfilesCondition.class)
public class MyConfig {
public @Bean
ExampleService service() {
ExampleService service = new ExampleService();
service.setMessage("hello, success!");
return service;
}
}