Home > Software design >  How to read a value of type Long from a application.yml
How to read a value of type Long from a application.yml

Time:11-02

I am working with redis cache. I have in my code

Where the time is hardcoded

@RedisHash(timeToLive=60)

In applications.yml File I would like to add the following

throttle: 
  cache: 
    rate: 60 

So I can have the following

@RedisHash(timeToLive="${throttle.cache.rate}")

But this failed with Type mismatch cannot convert from String to Long. So I tried

@RedisHash(timeToLive="#{new Long(}'${throttle.cache.rate}')")

But still failing with Type mismatch cannot convert from String to Long.

CodePudding user response:

Basically, that is not possible. Why? Because the value for an annotation must be a compile-time constant. All the compiler knows at compile time is that you are passing a String to a property that only accepts long.

It will only be a long when you run your Spring application but that is too late because the compiler does not know that.

  • Related