使用Spring从Properties文件加载嵌套占位符

las*_*ase 7 java spring properties-file

是否可以从属性文件中加载嵌套占位符?我正在尝试动态加载URL.

例如,如果我的属性文件包含

my.url=http://localhost:8888/service/{nestedProperty}/
Run Code Online (Sandbox Code Playgroud)

有没有办法在运行时加载{nestedProperty}的值?与ResourceBundle的行为类似.如果是这样,我将如何有效地实例化String?到目前为止,我在想

<bean id="myURLString" class="java.lang.String" scope="prototype" lazy-init="true">
    <property name="URL" value="${my.url}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

...但我不确定要嵌套什么属性.如果可能的话,我想使用Annotations获取bean,尽管我目前有一些东西

ctx.getBean("myURLString", String.class, new Object[] { nestedProperty} );
Run Code Online (Sandbox Code Playgroud)

我在这里查看了PropertyPlaceholderConfigurer和其他几个属性文件问题,但我似乎无法弄清楚这是否可行.

我还应该注意,我想从我的代码中动态加载这个嵌套属性,或者至少从那里操作它们(可能通过@PostConstruct?)

Tom*_*yre 9

对的,这是可能的:

my.url=http://localhost:8888/service/${nestedProperty}
nestedProperty=foo/bar/baz
Run Code Online (Sandbox Code Playgroud)

在您的示例中添加大括号前面的美元符号,然后就可以了!

要实际使用完全解析的属性,请执行以下操作:

@Value("${my.url}")
private String url;
Run Code Online (Sandbox Code Playgroud)

在一个Spring bean中.