如何有条件地不在SpringBoot中创建bean?
在我的应用程序中,我有一个组件,在应用程序启动时从其他系统读取数据。 但是,在测试期间,我不希望创建此组件如何有条件地不在SpringBoot中创建bean?,spring,spring-boot,testing,javabeans,Spring,Spring Boot,Testing,Javabeans,在我的应用程序中,我有一个组件,在应用程序启动时从其他系统读取数据。 但是,在测试期间,我不希望创建此组件 @Component @Slf4j public class DeviceStatisticsSyncHandler { @EventListener public void handle(ApplicationReadyEvent event) { syncDeviceStatisticsDataSync(); } @Value("${t
@Component
@Slf4j
public class DeviceStatisticsSyncHandler {
@EventListener
public void handle(ApplicationReadyEvent event) {
syncDeviceStatisticsDataSync();
}
@Value("${test.mode:false}")
public boolean serviceEnabled;
}
我可以使用条件来解决这个问题,但其他代码读者需要理解,所以我认为这不是一个很好的方法:
@EventListener(condition = "@deviceStatisticsSyncHandler .isServiceEnabled()")
public void handle(ApplicationReadyEvent event) {
syncDeviceStatisticsDataSync();
}
public boolean isServiceEnabled() {
return !serviceEnabled;
}
@Value("${test.mode:false}")
public boolean serviceEnabled;
我的应用程序不使用配置文件,是否有其他方法来解决此问题
Spring Boot版本:2.1.3如果处于测试模式,一个可能的选项是根本不加载
DeviceStaticsSyncHandler
。
“test.mode”在这里不是一个好名字,因为生产代码包含与测试紧密绑定的内容
以下方法如何:
@Component
@ConditionalOnProperty(name ="device.stats.handler.enabled", havingValue = "true", matchIfMissing=true)
public class DeviceStatisticsSyncHandler {
// do whatever you need here, but there is no need for "test.mode" enabled related code here
}
现在,在测试中,您可以在测试本身上定义一个测试属性“device.stats.handler.enabled=false”,甚至可以将该定义放在src/test/reources/application.properties
中,这样模块中的所有测试都将是false
一个明显的优点是,这个定义几乎是不言自明的,并且可以被其他项目维护人员轻松理解 如果您处于测试模式,一个可能的选项是根本不加载
DeviceStaticsSyncHandler
。
“test.mode”在这里不是一个好名字,因为生产代码包含与测试紧密绑定的内容
以下方法如何:
@Component
@ConditionalOnProperty(name ="device.stats.handler.enabled", havingValue = "true", matchIfMissing=true)
public class DeviceStatisticsSyncHandler {
// do whatever you need here, but there is no need for "test.mode" enabled related code here
}
现在,在测试中,您可以在测试本身上定义一个测试属性“device.stats.handler.enabled=false”,甚至可以将该定义放在src/test/reources/application.properties
中,这样模块中的所有测试都将是false
一个明显的优点是,这个定义几乎是不言自明的,并且可以被其他项目维护人员轻松理解 对我来说,情况与环境无关。我将使用spring profile解决此问题 步骤1:首先创建一个接口
public interface DeviceStatisticsSyncHandler {
public void handle(ApplicationReadyEvent event);
}
步骤2:为生产创建实施
@Component
@Profile("!test")
public class DeviceStatisticsSyncHandlerImpl implements DeviceStatisticsSyncHandler {
@EventListener
@Override
public void handle(ApplicationReadyEvent event) {
syncDeviceStatisticsDataSync();
}
}
步骤3:创建测试的实现
@Component
@Profile("test")
public class DeviceStatisticsSyncHandlerTestImpl implements DeviceStatisticsSyncHandler {
@EventListener
@Override
public void handle(ApplicationReadyEvent event) {
//do Nothing
}
}
最后一步
您只需设置/切换属性
-Dspring.profiles.active=test
或
-Dspring.profiles.active=prod
对我来说,情况并非如此,而是与环境有关。我将使用spring profile解决此问题 步骤1:首先创建一个接口
public interface DeviceStatisticsSyncHandler {
public void handle(ApplicationReadyEvent event);
}
步骤2:为生产创建实施
@Component
@Profile("!test")
public class DeviceStatisticsSyncHandlerImpl implements DeviceStatisticsSyncHandler {
@EventListener
@Override
public void handle(ApplicationReadyEvent event) {
syncDeviceStatisticsDataSync();
}
}
步骤3:创建测试的实现
@Component
@Profile("test")
public class DeviceStatisticsSyncHandlerTestImpl implements DeviceStatisticsSyncHandler {
@EventListener
@Override
public void handle(ApplicationReadyEvent event) {
//do Nothing
}
}
最后一步
您只需设置/切换属性
-Dspring.profiles.active=test
或
-Dspring.profiles.active=prod
如果满足某些条件,将bean加载到应用程序上下文中是一种好方法,我使用这种方法在测试和产品环境中加载不同的数据源,我决定使用这种方法。非常感谢。如果满足某些条件,将bean加载到应用程序上下文中是一个好方法,我使用这种方法在测试和产品环境中加载不同的数据源,我决定使用这种方法。非常感谢你。