문제

I am trying to figure out how to use Spring Profiles for testing. 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?

도움이 되었습니까?

해결책

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]. In your case String. You will not get your configured map.

See: 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. To get your bean you will either have to put an @Qualifier("profileDependentProps") next to the @Autowired or use @Resource("profileDependentProps") instead of @Autowired.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top