3

How to get cron from application.yml.

application.yml:

cron-expression:
  every: ${CRON:* * * ? * *}

DTO:

@Configuration
@ConfigurationProperties(prefix = "cron-expression")
class ConfigDTO {
    String every;
       
    public String getEvery(){
        return every;

    }
}

Schedule

public Class Schedule{

@Autowired
private ConfigDTO dto;

    @Scheduled(cron = dto.getEvery())
    public void test(){
       Systems.out.println("date time",newDate());
    }
    
    }

Is there any possibility to handle like above

  • 1
    [This answer also help to your questin](https://stackoverflow.com/questions/39642170/pass-a-yaml-based-property-value-to-scheduled-annotation-in-spring-boot/39642594) – dileep balineni Jul 15 '20 at 13:47

1 Answers1

1

You can use SPEL (Spring Expression Language) in @Schedule expression

 @Scheduled(cron = "${cron-expression. every}")
 public void test(){
   Systems.out.println("date time",newDate());
 }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98