DECEMCLMC
DECEMCLMC

Reputation: 115

How does Spring interpolate ${x} inside a string?

I have a java class in a Spring project that looks (edited) like:

@Component
public class X
{
private static final ApplicationContext CTX = new FileSystemXmlApplicationContext("file:${PATH}/ApplicationContext.xml");
...

I am looking for the reference explaining how the ${PATH} is interpolated in the string parameter. The PATH is passed as a system property ( java -DPATH=...) so I assume it takes it from there but I can't find an explanation describing the mechanism. Is it a Spring related feature similar to the syntax used in @Value?

Upvotes: 4

Views: 2100

Answers (1)

Andreas
Andreas

Reputation: 159086

configLocations (type String) passed to one of the FileSystemXmlApplicationContext constructors are processed by the resolvePath() method inherited from the AbstractRefreshableConfigApplicationContext class.

resolvePath() documentation says:

Resolve the given path, replacing placeholders with corresponding environment property values if necessary. Applied to config locations.

See Also:
PropertyResolver.resolveRequiredPlaceholders(String)

resolveRequiredPlaceholders() documentation says:

Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by getProperty(java.lang.String). Unresolvable placeholders with no default value are ignored and passed through unchanged.

The PropertyResolver declaring that getProperty() method is actually a StandardEnvironment.

StandardEnvironment documentation says:

Environment implementation suitable for use in 'standard' (i.e. non-web) applications.

In addition to the usual functions of a ConfigurableEnvironment such as property resolution and profile-related operations, this implementation configures two default property sources, to be searched in the following order:

Upvotes: 3

Related Questions