RandomValuePropertySource class final

A property source that makes up a new random value for every key that starts with random..

It lets configuration ask for a value that differs per run, such as an instance id or a port for a test server: id=${random.uuid}. SpringApplication adds it, so config files can use ${random.*} without any code; an application calls addToEnvironment only for an environment it builds itself. The keys are:

  • random.int, random.long: any int (a long is 64 bits on the VM and at most 53 bits on the web);
  • random.int(10), random.int[5,10], random.long(100): in a range, the lower bound included and the upper one excluded;
  • random.uuid: a random version 4 UUID;
  • any other random.* (random.value): 32 random hex digits.

Every read gives a new value, so read a key once and keep the result when it must stay the same. A malformed range throws when it is read.

// An environment built by hand, for a tool that names its run.
RandomValuePropertySource.addToEnvironment(environment);
environment.getProperty('random.int[1,7]'); // e.g. '4'
environment.resolvePlaceholders(r'app-${random.uuid}');

Spring (Java):

RandomValuePropertySource.addToEnvironment(environment);
environment.getProperty("random.int[1,7]");
Inheritance

Constructors

RandomValuePropertySource({String name = randomPropertySourceName, Random? random})
Creates the source called name (random unless given). random draws the values; without it a secure generator is created on first use. Pass a seeded Random in tests for repeatable values.

Properties

hashCode → int
The hash code for this object.
no setterinherited
name → String
The name of this source, unique within the environment's list. It is how other code finds, replaces or orders the source, and it appears in error messages (Config resource 'classpath:/application.yml').
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
source → Object
The object the values are read from: a map, the command-line arguments, a client.
finalinherited

Methods

containsProperty(String name) → bool
Whether this source has the property name. By default, whether getProperty returns a value other than null.
inherited
getProperty(String name) → Object?
The value of the property name, or null when this source does not have it (the environment then asks the next source).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
Whether other is a source with the same name, whatever its values, as in Spring.
inherited

Static Methods

addToEnvironment(ConfigurableEnvironment environment) → void
Adds a random value source to environment, right after the environment variables (or last when there are none), unless it has a source called random already.

Constants

randomPropertySourceName → const String
The name of the source, as in Spring Boot: random.