user1093127
user1093127

Reputation: 21

Accessing property values in Annotations

I want to access a property value in my annotation, as an attribute's value.

For ex. in my property file I have an entry expression: 3/10 * * * * ?.

In my Scheduler class I use annotation @Scheduled (cron = "**VALUE**"). I want to read this value from the properties file corresponding to the expression key.

Tried doing this with @Value, but it returns a type of Value which cannot be converted to String.

Upvotes: 1

Views: 3669

Answers (2)

James.G
James.G

Reputation: 101

From spring 3.0.1 you can do it like this @Scheduled(cron = "${rates.refresh.cron}")

Refer to http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression

However, you cannot do this for fixDelay and fixRate due to type casting problem (fixDelay expects value in long, while annotation return String only). Check Mark's comments in https://jira.springsource.org/browse/SPR-6670

Upvotes: 4

Shimi Bandiel
Shimi Bandiel

Reputation: 5747

You can try to use the APT (annotation processing tool) to replace the value in the annotation with a value from the property file.

Upvotes: 0

Related Questions