多配置文件的Spring Profiles问题

时间:2019-11-27 11:52:55

标签: spring spring-boot

Spring Profiles无法正常工作。我下面有两个豆。我要用于本地,开发和myDevProfile的第一个Bean。我添加了@Profile({"local", "dev", "mydev"}),但是它不起作用。我正在使用Spring Boot 2.0.1.RELEASE

@Bean
@Profile("local")
@Qualifier("myApiClient")
public ApiClient localxApiClient() {
    return new RestTemplateApiClient(baseUrl);
}

@Bean
@Profile("!local")
@Qualifier("myApiClient")
public ApiClient xApiClient() {
    return new ComplexClient(baseUrl);
}

1 个答案:

答案 0 :(得分:0)

这似乎对我有用:

@SpringBootTest
@ActiveProfiles("local")
public class LocalApiClientTests {
    @Autowired
    private ApiClient apiClient;

    @Test
    public void testApiClient() {
        assertThat(apiClient).isInstanceOf(RestTemplateApiClient.class);
    }
}
@SpringBootTest
@ActiveProfiles({"dev", "mydev"})
public class NonLocalApiClientTests {
    @Autowired
    private ApiClient apiClient;

    @Test
    public void testApiClient() {
        assertThat(apiClient).isInstanceOf(ComplexClient.class);
    }
}