简体   繁体   English

Spring Profiles奇怪的行为

[英]Spring Profiles odd behavior

I am trying to figure out how to use Spring Profiles for testing. 我试图弄清楚如何使用Spring Profiles进行测试。 I think I do everything according to spring docs. 我认为我根据春季文档做了一切。 And in the end I got results that I cannot explain Here is the listing of my program: 最后我得到的结果我无法解释这是我的程序列表:

Here is the main config: 这是主配置:

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;
}
}

Test configuration: 测试配置:

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;
}
}

And finally my test class: 最后我的测试类:

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());
    }
}
}

And odd output: 奇数输出:

[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

What's with this line? 这条线有什么用? --- helloMsg=Hello dev Where is my map? --- helloMsg = Hello dev我的地图在哪里?

By default when Spring encounters a auto wiring field of type Map<String, [type]> it will inject a map of beans of the specific [type] . 默认情况下,当Spring遇到Map<String, [type]>类型的自动布线字段时,它将注入特定[type]的bean映射。 In your case String . 在你的情况下String You will not get your configured map. 您将无法获得配置的地图。

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

You are basically running into a corner case as you have a map with String as keys. 你基本上遇到了一个角落的情况,因为你有一个带有String作为键的地图。 To get your bean you will either have to put an @Qualifier("profileDependentProps") next to the @Autowired or use @Resource("profileDependentProps") instead of @Autowired . 要获得你的bean,你要么必须在@Autowired旁边放一个@Qualifier("profileDependentProps") ,要么使用@Resource("profileDependentProps")而不是@Autowired

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM