Spring @Scheduled task runs twice

后端 未结 3 1297
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 09:05

I am creating an @Scheduled task to run every 5 seconds. As has been a problem in other questions, my task is running twice!

I have looked at other ques

3条回答
  •  自闭症患者
    2020-12-11 09:44

    I also had this problem. I am using Spring 4. I have no xml configuration. Everything is configured with annotations and Java config.

    I have a base configuration and a WebConfiguration. the error was caused by using @ComponentScan in both configurations. I removed component scan from base configuration.

    @EnableWebMvc
    @ComponentScan(basePackages = { "com.myservice" })
    @Configuration
    public class WebConfiguration extends WebMvcConfigurerAdapter {
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
      }
    
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
      }
    }
    
    @Configuration
    @EnableScheduling
    //@ComponentScan(basePackages = { "com.myservice" })  
    @PropertySource("${myservice.properties.location:classpath:myservice.properties}")
    public class BaseConfiguration  {
    
    
      @Autowired
      Environment environment;
    
      @Bean
      public static PropertySourcesPlaceholderConfigurer properties() {
        return new PropertySourcesPlaceholderConfigurer();
      }
    

提交回复
热议问题