0

I use Spring 4. I run Spring context like:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.scan(BASE_PACKAGE);
    context.refresh();

I use injecting of environment variables like:

@Value("#{systemEnvironment['ENV_VAR'] ?: 60}")
private int someDelay;

And it works pretty fine.

The question is how to make this work:

@Scheduled(fixedDelayString = "${ENV_VAR ?: 60000}")
public void runSomeTask(){
    System.out.println("hello ");
}

Now spring throws exception:

Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'runSomeTask': Invalid fixedDelayString value "${ENV_VAR ?: 60000}" - cannot parse into integer

I suppose I need to create a BeanPostProcessor which is responsible for processing such values, but I have no idea which one exactly.

P.S. It works fine with Spring Boot out of the box.

  • 1
    What is wrong with `Scheduled(fixedDelayString = "("#{systemEnvironment['ENV_VAR'] ?: 60000}")`? – Jens Jan 17 '18 at 12:06
  • @Jens the same exception actually as with `"${ENV_VAR ?: 60000}"` –  Jan 17 '18 at 12:25
  • Can you Show the exception text – Jens Jan 17 '18 at 12:38
  • Alternatively, you can do something like this https://stackoverflow.com/a/2602676/1654233 – yegodm Jan 17 '18 at 14:21
  • @Jens `Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'runSomeTask': Invalid fixedDelayString value "#{systemEnvironment['ENV_VAR'] ?: 60000}" - cannot parse into integer` –  Jan 17 '18 at 15:00
  • It's not possible – ACV Jan 18 '18 at 14:46

1 Answers1

0

Try with this :

#{'${ENV_VAR}' == null ? '60000' : '${ENV_VAR}' }

harsh tibrewal
  • 785
  • 6
  • 21