[英]entry value of a map depends on environment variable in spring
我在應用程序上下文xml中有一個spring map聲明,如下所示:
<map>
<entry key="mykey">
<value>${${env}.userkey}</value>
</entry>
</map>
然而,價值永遠不會被環境所取代。 有沒有辦法做到這一點?
謝謝,
肖恩
如果您使用的是Spring 3
在Spring CONFIGS你可以使用規划環境地政司 ,以及你所需要的可能是systemProperties這樣的:
<map>
<entry key="mkey" value="#{ systemProperties['env'] + '.userkey' }" />
</map>
#{systemProperties}是否支持環境變量? 什么是使用PropertyPlaceholderConfigurer( 鏈接 )
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:env.properties" />
</bean>
<util:map id="map">
<entry key="mykey">
<value>${${env}.userkey}</value>
</entry>
</util:map>
env.properties:
local.userkey=Me
單元測試:
package org.test;
import static org.junit.Assert.assertEquals;
import java.util.Map;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class EnvTest {
@Resource
private Map<String, String> map;
@Test
public void testMap() throws Exception {
assertEquals("Me", map.get("mykey"));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.