问题
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.
回答1:
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
}
}
}
来源:https://stackoverflow.com/questions/21917626/replace-scheduledannotationbeanpostprocessor-in-integration-tests