Search code examples
javaspringspring-java-configspring-scheduled

@Scheduled is not working with Javaconfig


I have written one scheduler which is working as expected only with xml file. But I am unable to run this with Javaconfig class. Following is the code.

Scheduler:

public class DemoServiceBasicUsageCron {    
@Scheduled(cron="*/5 * * * * ?")
public void demoServiceMethod()
{
    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
}}

Java config:

@Configuration
public class TestCron {
    @Bean
    public DemoServiceBasicUsageCron demoCron() {
        System.out.println(" bean created ");
        return new DemoServiceBasicUsageCron();
    }}

I am reading the configuration file as

public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestCron.class);

    }

Needed any suggestions which it can work.

Regards Sai


Solution

  • Add @EnableScheduling annotation in TestCron class.