Spring boot 是否可以仅基于概要文件加载特定注释?

Spring boot 是否可以仅基于概要文件加载特定注释?,spring-boot,unit-testing,Spring Boot,Unit Testing,是否可以仅在测试期间或仅在Spring Boot中运行期间加载特定注释 我面临的情况是,有注释会影响测试,但在实时运行中工作得很好,因此我想知道是否可以仅在测试期间排除它们,而在运行时包含它们,类似于如何基于Spring概要文件包含特定bean 抱歉如果以前有人问过这个问题,我尝试过搜索,但没有任何效果。您可以使用@ConditionalOnProperty注释,该注释根据设置的属性(在应用程序中.properties->app.val=false)创建bean。例如,对于服务: @Servic

是否可以仅在测试期间或仅在Spring Boot中运行期间加载特定注释

我面临的情况是,有注释会影响测试,但在实时运行中工作得很好,因此我想知道是否可以仅在测试期间排除它们,而在运行时包含它们,类似于如何基于Spring概要文件包含特定bean


抱歉如果以前有人问过这个问题,我尝试过搜索,但没有任何效果。您可以使用
@ConditionalOnProperty
注释,该注释根据设置的属性(在
应用程序中.properties
->
app.val=false
)创建bean。例如,对于服务:

@Service
@ConditionalOnProperty(name = "app.val", havingValue = "false")
public class TestService {
   ...
}
您还可以使用
@Profile
注释,并将它们注释到具有测试配置文件(在
应用程序.properties
以及->
spring.profiles=test
中定义)的方法中

@Profile({"test"})
public String getValue() {
    return "test value";
}
@Profile({"production"})
public String getValue() {
    return "production value";
}