在集成测试中替换ScheduledAnnotationBeanPostProcessor [英] Replace ScheduledAnnotationBeanPostProcessor in integration tests

查看:941
本文介绍了在集成测试中替换ScheduledAnnotationBeanPostProcessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个使用@Scheduled注释来执行各种工作的Spring应用程序。在prod中,它很好用。然而,这个特性在运行spock集成测试时会导致一些问题 - 一旦容器启动,我们所有的任务都会被解雇,并且会让我们的测试运行变得糟糕。



我寻找一种方法来关闭调度功能,但仍然有容器(配置为@ComponentScan)将其作为普通的'ol bean'选取。



基于在我迄今为止所做的一些调整工作中,似乎如果我可以使用no-op实现覆盖内置的ScheduledAnnotationBeanPostProcessor,我可以实现此目标..但是,当我在容器中创建此bean(使用@Bean创建(scheduledAnnotationBeanPostProcessor - 见下面的代码部分),它似乎被添加到BeanPostProcessors列表中 - 它仍然包含原始实现。

  @Bean(name =scheduledAnnotationBeanPostProcessor)
ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor(){
返回新的Schedul edAnnotationBeanPostProcessor(){
@Override
public Object postProcessAfterInitialization(final Object bean,String beanName){
return bean
}
}







$ b因此,我想我的问题是 - 我怎样才能连接一个bean会替换一个内置的BeanPostProcessor? FYI我使用的是Spring 3.2.4,并且通过Spring注释100%配置了应用程序。

h2_lin>解决方案

我的错误是我没有正确地命名bean。我最终找到了这个bean的构建位置(在org.springframework.scheduling.annotation.SchedulingConfiguration中)并且我复制了它的配置。
$ b

这个方法演示了正确的名称/配置:

  @Bean(name = AnnotationConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
BeanPostProcessor scheduledAnnotationBeanPostProcessor(){
返回新的BeanPostProcessor(){
@Override
对象postProcessBeforeInitialization(Object bean,String beanName)抛出BeansException {
return bean
}

@Override
Object postProcessAfterInitialization(Object bean,String beanName)throws BeansException {
return bean
}
}
}


So, I've got a spring application that uses the @Scheduled annotation to do various jobs. In prod, it works great. However this feature causes us some problems when running spock integration tests-as soon as the container starts up, all our tasks are fired and it mucks up our test runs.

I'm looking for a way to turn off the scheduling functionality, but still have the container (configured with @ComponentScan) pick it up as a regular 'ol bean.

Based on some legwork I've done so far, it seems that if I could override the built-in ScheduledAnnotationBeanPostProcessor with a no-op implementation I could achieve this goal..but when I create this bean in the container(created using the @Bean("scheduledAnnotationBeanPostProcessor"-see the code section below), it just seems to be added to the list of BeanPostProcessors-which still contains the original implementation.

   @Bean(name="scheduledAnnotationBeanPostProcessor")
    ScheduledAnnotationBeanPostProcessor  scheduledAnnotationBeanPostProcessor(){
        return new ScheduledAnnotationBeanPostProcessor(){
            @Override
            public Object postProcessAfterInitialization(final Object bean, String beanName){
                return bean
            }
        }


    }

So, I guess my question is-how can I wire in a bean that will replace a built-in BeanPostProcessor? FYI I'm using Spring 3.2.4 and the application is configured 100% via Spring annotations.

thanks.

解决方案

My mistake was that I didn't name the bean correctly. I ended up finding where this bean was being built(in org.springframework.scheduling.annotation.SchedulingConfiguration) and I copied it's configuration.

This method demonstrates the proper names/config:

@Bean(name=AnnotationConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
BeanPostProcessor scheduledAnnotationBeanPostProcessor(){
    return new  BeanPostProcessor(){
        @Override
        Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean
        }

        @Override
        Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            return bean
        }
    }
}

这篇关于在集成测试中替换ScheduledAnnotationBeanPostProcessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆