How can I get one of my spring configuration variables in an annotation that not inside of a class scope? Currently, I am doing this:
@Data
@RedisHash(value = "MyEntity", timeToLive = 604800 )
public class MyEntity{
private String id;
private String name;
}
but what I really want to do is this (or something equivalent):
@Data
@RedisHash(value = "MyEntity", timeToLive = @Value("${spring.redis.expire}") )
public class MyEntity{
private String id;
private String name;
}
Any solutions which allow me to access my config variables in my application.yml in my annotations (in this case, completing the value for my @RedisHash annotation)???
2
Answers
It turns out you can assign a configurable expiration value by using a
@TimeToLive
annotation inside of your@RedisHash
annotated entity class. In my case, it looks like this:Then in the implementation that uses the redis entity, you simply assign that expiration value [
myEntity.setExpiration(expiration);
], as in the following code:I guess this should work. Use property without @Value annotation.