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);
}
答案 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);
}
}