如何从测试应用程序上下文中排除 spring 引导应用程序 bean?

How to exclude spring boot application beans from the test App context?

我目前定义我的基本规格如下:

@Configuration
@ActiveProfiles(profiles = "test")
@ContextConfiguration(locations = "classpath:test-config-context.xml" )
class SpringSharedSpecification extends Specification 

这也最终处理了我的父应用程序-context.xml

我正在使用活动配置文件选项来不加载主要 bean 并使用 test-config 执行 我在下面用来跳过应用程序上下文 bean,但我觉得这不是最好的方法。谁能建议更好的跳过应用程序的方法-context.xml?

<beans profile="!test"> 

提前感谢您的回答。

您可以在 bean 上使用注解 class:

@Component
@Profile("!test")
class MyProductionBean {
}