spring-boot: RandomValuePropertySource returns a different random value each time a random property is referenced

Currently, randomized configuration properties produce new randomized values each time they are referenced within config.

For example, with the following yaml configuration:

my:
    random: ${random.value}
another:
    property: ${my.random}

my.random and another.property will be two different randomized values when pulled in via @Value, for example.

Even using the same property twice will result in two different values:

@Value("${my.random}")
String random1;
@Value("${my.random}")
String random2;

In the above case, random1 will not equal random2.

It seems like it would be useful and more consistent for the my.random property to be randomized but reflect the same random value no matter where, how, or how many times it is referenced.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 15 (6 by maintainers)

Most upvoted comments

I’m not keen on making the property source stateful. I’d rather bind a single random value into a @ConfigurationProperties annotated bean and then use that bean to configure anything else that needed the same value.

We’ve discussed this today and the Boot team are in agreement that the property source should not be stateful.

The proposed solution does not work if you have no control on where the random value is accessed. My error scenario was something like that: Spring Boot application with Discovery Client, and

server.port: ${random.int[10000,65000]}

Eureka Client would be receiving a random port that was not the real port I was starting. Since server.port: 0 is already not working, I was testing this solution