我在应用程序上下文 xml 中声明了一个 spring 映射,如下所示:
<map>
<entry key="mykey">
<value>${${env}.userkey}</value>
</entry>
</map>
然而,值(value)永远不会被环境所取代。有办法做到这一点吗?
谢谢,
肖恩
最佳答案
#{systemProperties} 是否支持环境变量?使用 PropertyPlaceholderConfigurer 有什么意义 ( link )
<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>
环境属性:
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"));
}
}
关于java - map 的入口值取决于 Spring 的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8099799/