Atom999
Atom999

Reputation: 452

Is there a way to use Spring SpEL inside a method to get a .properties value?

I have a scheduled task class with @Component annotation. I have this successfully pulling data from the .properties file for the delay time, but I'd like to use that same value later INSIDE the method.

@Scheduled(fixedDelayString = "${mypropvalue}")
public void doScheduledTask () throws IOException
    {
     // do some stuff

    log.info("The doScheduledTask finished at {} ", dateFormat.format(new Date()));
    log.info("The next task will run in {} ms", @Value("${mypropvalue}"));
    }

The @Value on the last line has a compile error saying "Annotations are not allowed here". How can I get that value again from inside the method? And since I am using @Scheduled, I cannot pass in that @Value as a parameter.

Upvotes: 0

Views: 326

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

Add it as a field

@Value("${mypropvalue}
private long fixedDelay

then

   this.fixedDelay

within your method.

Upvotes: 1

Related Questions