scheduled java 失败_Java SpringBoot方法仍然在错误的@Profile下@Sche...

我的application.properties文件将默认配置文件定义为spring.profiles.active = test,我有一个计划的方法,如下所示:

@Scheduled(initialDelay = 2500, fixedRate = 60 * 1000 * minutesRecheckRate)

@Profile("loop")

public void processingLoop() {

System.out.println(Arrays.toString(env.getActiveProfiles()));

//.. the rest is omitted for brevity.

据我所知,在这种情况下,我应该永远不会在运行单元测试时看到此调用,因为我没有更改默认配置文件.事实并非如此,因为仍在计划中,我看到了输出

[test]

尽管我已尽力防止出现这种情况,但仍在控制台中.怎么了?为什么即使使用其他活动配置文件,它仍然可以运行?

更新:

由于这是与工作相关的应用程序,因此我无法提供更多信息,但我会尽力而为.

该类的配置如下:

@Configuration

@EnableScheduling

public class BatchConfiguration {

单元测试都是这样标注的:

@SpringApplicationConfiguration(classes = SpringBatchJsontestApplication.class)

public class SpringBatchJsontestApplicationTests extends AbstractTestNGSpringContextTests {

主要的应用程序类是这样的:

@SpringBootApplication

public class SpringBatchJsontestApplication {

他们都没有改变任何其他东西.没有context.xml文件,这是一个SpringBoot应用程序,因此所有内容仅是注释.

这是对我来说效果很好的最终结果

@Profile("test")

@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)

@Role(BeanDefinition.ROLE_INFRASTRUCTURE)

public ScheduledAnnotationBeanPostProcessor scheduleBeanProcessorOverride() {

logger.info("Test Profile is active, overriding ScheduledAnnotationBeanPostProcessor to prevent annotations from running during tests.");

return new ScheduledAnnotationBeanPostProcessor() {

@Override

protected void processScheduled(Scheduled scheduled, Method method, Object bean) {

logger.info(String.format("Preventing scheduling for %s, %s, %s", scheduled, method, bean.getClass().getCanonicalName()));

}

};

}

这是触发测试配置文件的POM配置,因此我不再需要在我的application.properties中明确地这样做.

org.apache.maven.plugins

maven-surefire-plugin

2.19

test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值