java - map 的入口值取决于 Spring 的环境变量

标签 java spring

我在应用程序上下文 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/

相关文章:

spring - 在一页Struts中显示数据表并提交表单

java - 没有可用的类型 [...] 的合格 Bean

java - 根据消息类型选择消息实现

Java: equalsIgnoreCase + message.startswith?

java - 使用 ContributesAndroidInjector 提供 Activity

Java在windows tomcat上部署时不提取war文件并抛出异常

mysql - 为什么关联集合包含空值? (Hibernate, Annotation, Spring)

java - Jfreechart - 我们可以在 StackedAreaChart 中为数据点设置形状吗?

java - 忽略 Infinitest 中的测试

java - 从 Rest Controller 返回对象层次结构时如何使用 Spring MVC @JsonView