我想知道是否有一种方法可以根据活动配置文件不启动spring boot应用程序。原因是,我有两个配置文件:本地和集成。当配置文件是本地的时,我将在本地启动该应用程序。如果配置文件是集成的,我希望测试不启动应用程序,而是使用在配置类中设置的端点,以便它实际上使用已部署的spring boot应用程序。
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
)
@ContextHierarchy({
@ContextConfiguration(
initializers = ConfigFileApplicationContextInitializer.class,
classes = TestConfiguration.class,
loader = SpringBootContextLoader.class
),
@ContextConfiguration(
initializers = ConfigFileApplicationContextInitializer.class,
classes = Application.class,
loader = SpringBootContextLoader.class
),
}
)
public class SpringContextConfiguration {
@Before
public void init(){}
}
答案 0 :(得分:0)
在Spring Boot中,您可以使用@Profile
批注。您只需定义两个@Beans
实现Foo
接口,这些接口仅在启动时创建,然后将它们注入仅在您的Bar
中创建的don't start profile
。
@Profile('dont')
@Bean
class Bar {
private Foo foo;
public Bar(Foo foo) {
this.foo = foo;
}
}
interface Foo {
}
@Bean
@Profile('dont')
class Foo1 implements Foo {
}
@Bean
@Profile('dont')
class Foo2 implements Foo {
}
这将导致模棱两可,当设置配置文件dont
时,将阻止您的应用程序启动。