Spring Profiles奇怪的行为

时间:2014-03-10 13:44:04

标签: java spring testing spring-profiles

我正在试图弄清楚如何使用Spring Profiles进行测试。我认为我根据春季文档做了一切。最后我得到的结果我无法解释这是我的程序列表:

这是主配置:

package com.test.profiles;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ComponentScan("com.test.profiles")
public class MainConfig {

@Autowired
private String helloMsg;

@Autowired
private Map<String,String> profileDependentProps;

@Bean
@Profile("default")
public String helloMsg() {
    return "Hello default";
}

@Bean
@Profile("default")
public Map<String,String> profileDependentProps() {
    Map<String,String> props = new HashMap<>();
    props.put("1", "default");
    props.put("2", "default");
    props.put("3", "default");
    return props;
}
}

测试配置:

package com.test.profiles;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Profile("dev")
public class TestConfig {

@Bean
public String helloMsg() {
    return "Hello dev";
}

@Bean
public Map<String,String> profileDependentProps() {
    Map<String,String> props = new HashMap<>();
    props.put("1", "dev");
    props.put("2", "dev");
    props.put("3", "dev");
    return props;
}
}

最后我的测试班:

package com.test.profiles;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.Map;

@ContextConfiguration(classes = MainConfig.class)
@ActiveProfiles("dev")
public class ProfileTester extends AbstractTestNGSpringContextTests {

@Autowired
private String string;

@Autowired
private Map<String,String> profileDependentProps;

@Test
public void profileTest() {
    System.out.println("TEST: "+string);
    for(Map.Entry<String,String> entry : profileDependentProps.entrySet()) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
}
}

奇数输出:

[TestNG] Running:
C:\Users\nikopol\.IntelliJIdea13\system\temp-testng-customsuite.xml

15:36:34,893  INFO GenericApplicationContext:513 - Refreshing org.springframework.context.support.GenericApplicationContext@7ecdc69b: startup date [Mon Mar 10 15:36:34 EET 2014]; root of context hierarchy

TEST: Hello dev
helloMsg=Hello dev

===============================================
Custom suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

15:36:35,105  INFO GenericApplicationContext:873 - Closing org.springframework.context.support.GenericApplicationContext@7ecdc69b: startup date [Mon Mar 10   15:36:34 EET 2014]; root of context hierarchy

这条线有什么用? --- helloMsg = Hello dev 我的地图在哪里?

1 个答案:

答案 0 :(得分:3)

默认情况下,当Spring遇到Map<String, [type]>类型的自动布线字段时,它将注入特定[type]的bean映射。在您的情况下String。您将无法获得配置的地图。

请参阅:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-autowired-annotation

您基本上遇到了一个角落案例,因为您有一个以字符串为键的地图。要获得您的bean,您必须在@Qualifier("profileDependentProps")旁边放置@Autowired或使用@Resource("profileDependentProps")而不是@Autowired