0

Is it possible to set the parameter of an annotation not statically but by a variable or another annotation?

@Component
public class Test
{
  @Value ("${my.rate}")
  private int myrate;

  @Scheduled (fixedRate = myrate)       // like so
  public void run () 
  {

EDIT

What I try to do is pretty common - I think.

I have a cyclic action (run) and the rate of the cycle should be parametrized (in application.properties).

chris01
  • 10,921
  • 9
  • 54
  • 93
  • Possible duplicate of https://stackoverflow.com/q/16509065/4929038 – kidney Oct 06 '20 at 12:07
  • Does this answer your question? [Modify a class definition's annotation string parameter at runtime](https://stackoverflow.com/questions/14268981/modify-a-class-definitions-annotation-string-parameter-at-runtime) – Milgo Oct 06 '20 at 12:08

2 Answers2

0

Found out how to do it when the parameter is out of an annotation.

@Scheduled (fixedRateString = "${my.rate}")
public void run ()
{
chris01
  • 10,921
  • 9
  • 54
  • 93
  • 1
    the parameter of the annotation is still set statically (needs to be set at compile time). Why this works is, because Spring evaluates the parameter of the annotation at runtime and only then resolves this. – P.J.Meisch Oct 06 '20 at 12:38
0

It is not possible. It must be constant and to be populated on compile time