Inject @Scheduled fixedRate value from Spring Boot application.yml file

前端 未结 3 1592
星月不相逢
星月不相逢 2020-12-29 02:08

I know I can inject the value from properties file with the following syntax:

@Scheduled(fixedRate=${myRate})
public void getSchedule(){
    System.out.print         


        
相关标签:
3条回答
  • 2020-12-29 02:48

    In my application I use the annotation PropertySource on my config class:

    @PropertySource("application-${spring.profiles.active}.yml")

    spring.profiles.active returns the active profile (dev, test, etc). My properties file name is application-dev.yml

    The annotation @Scheduled works with property injection. Dont forget the annotation with prefix configuration on your class.

    0 讨论(0)
  • 2020-12-29 03:00

    In my application.properties (YAML) I put this

    console:
        fetchMetrics: 5000
    

    Then in my simple Task class I push the definition :

    @Scheduled(fixedRateString ="${console.fetchMetrics}", initialDelay=1000)
    public void fetchMetrics() {
        logger.info("What's up ?");
    }
    

    Please notice that fixedRate expects a long and you want to inject a placeholder, you will need fixedRateString

    0 讨论(0)
  • 2020-12-29 03:03

    I find it easy once done for my project.
    Change fixedRate to fixedRateString and put the property key in double quotes like this:

    @Scheduled(fixedRateString="${myRate}")
    public void getSchedule() {
        System.out.println("Scheduled job");
    }
    
    0 讨论(0)
提交回复
热议问题