Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在非Spring启动项目中使用@Scheduled注释_Java_Spring_Spring Mvc - Fatal编程技术网

Java 如何在非Spring启动项目中使用@Scheduled注释

Java 如何在非Spring启动项目中使用@Scheduled注释,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在开发一个没有Spring Boot的Spring项目 我没有使用xml配置,而是使用注释。 那么如何使用@Schedule(cron=“bla bla…”) 我应该把@EnableScheduling放在哪里。 下面是我的一段代码,它不起作用: @RestController public class MyController { @Scheduled(cron = "0 40 22 * * SUN") public void routinessundayBAS1() th

我正在开发一个没有Spring Boot的Spring项目 我没有使用xml配置,而是使用注释。 那么如何使用
@Schedule(cron=“bla bla…”)
我应该把
@EnableScheduling
放在哪里。 下面是我的一段代码,它不起作用:

@RestController
public class MyController {
    @Scheduled(cron = "0 40 22 * * SUN")
    public void routinessundayBAS1() throws EntityNotFoundException {
        System.out.println("coming");
    }
}

您需要在任何
@Configuration
类上添加
@EnableScheduling

正如Jakub所说,您必须在任何
@Configuration
类上添加
@EnableScheduling
。您可以通过实现
SchedulingConfigurer
接口来配置调度器以自定义配置。例如:

@Configuration
@EnableScheduling
@ComponentScan("PACKAGES WITH SCHEDULED ANNOTATIONS")
public class ConfigScheduler implements SchedulingConfigurer {
    ...    
    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {

       ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
       scheduler.setPoolSize( threadpoolsize );
       scheduler.setThreadGroupName( threadgroupname );
       scheduler.setThreadNamePrefix( threadPrefix );
       scheduler.setAwaitTerminationSeconds( timeout );    
       return scheduler;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar registrar) {      
        TaskScheduler scheduler = this.taskScheduler();
        registrar.setTaskScheduler( scheduler );
    }
    ...
}