Java 弹簧外形异常

Java 弹簧外形异常,java,spring,testing,spring-profiles,Java,Spring,Testing,Spring Profiles,我试图弄清楚如何使用Spring概要文件进行测试。我想我做的每一件事都是根据spring文档。最后我得到了我无法解释的结果,这里是我的程序列表: 以下是主要配置: package com.test.profiles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframewor

我试图弄清楚如何使用Spring概要文件进行测试。我想我做的每一件事都是根据spring文档。最后我得到了我无法解释的结果,这里是我的程序列表:

以下是主要配置:

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;
}
}
这条线是怎么回事helloMsg=Hello dev
我的映射在哪里?

默认情况下,当Spring遇到类型为
map
的自动布线字段时,它将注入特定
[type]
的bean映射。在您的情况下,
字符串
。您将无法获取已配置的映射

请参阅:

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

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