Spring 如何在字符串中插入 ${x}?
How does Spring interpolate ${x} inside a string?
我在 Spring 项目中有一个 java class,看起来(已编辑)如下:
@Component
public class X
{
private static final ApplicationContext CTX = new FileSystemXmlApplicationContext("file:${PATH}/ApplicationContext.xml");
...
我正在寻找解释如何在字符串参数中插入 ${PATH} 的参考资料。 PATH 作为系统 属性 ( java -DPATH=...) 传递,所以我假设它是从那里获取的,但我找不到描述该机制的解释。它是一个与@Value 中使用的语法相似的 Spring 相关功能吗?
configLocations
(类型 String
)传递给 FileSystemXmlApplicationContext
constructors are processed by the resolvePath()
method inherited from the AbstractRefreshableConfigApplicationContext
class.
之一
resolvePath()
文档说:
Resolve the given path, replacing placeholders with corresponding environment property values if necessary. Applied to config locations.
See Also:
PropertyResolver.resolveRequiredPlaceholders(String)
resolveRequiredPlaceholders()
文档说:
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.
PropertyResolver
declaring that getProperty()
method is actually a StandardEnvironment
.
StandardEnvironment
文档说:
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:
我在 Spring 项目中有一个 java class,看起来(已编辑)如下:
@Component
public class X
{
private static final ApplicationContext CTX = new FileSystemXmlApplicationContext("file:${PATH}/ApplicationContext.xml");
...
我正在寻找解释如何在字符串参数中插入 ${PATH} 的参考资料。 PATH 作为系统 属性 ( java -DPATH=...) 传递,所以我假设它是从那里获取的,但我找不到描述该机制的解释。它是一个与@Value 中使用的语法相似的 Spring 相关功能吗?
configLocations
(类型 String
)传递给 FileSystemXmlApplicationContext
constructors are processed by the resolvePath()
method inherited from the AbstractRefreshableConfigApplicationContext
class.
resolvePath()
文档说:
Resolve the given path, replacing placeholders with corresponding environment property values if necessary. Applied to config locations.
See Also:
PropertyResolver.resolveRequiredPlaceholders(String)
resolveRequiredPlaceholders()
文档说:
Resolve
${...}
placeholders in the given text, replacing them with corresponding property values as resolved bygetProperty(java.lang.String)
. Unresolvable placeholders with no default value are ignored and passed through unchanged.
PropertyResolver
declaring that getProperty()
method is actually a StandardEnvironment
.
StandardEnvironment
文档说:
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: