gpt4 book ai didi

java - Spring Profiles 奇怪的行为

转载 作者:行者123 更新时间:2023-11-28 20:23:47 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用 Spring Profiles 进行测试。我想我根据 spring 文档做所有事情。最后我得到了无法解释的结果这是我的程序 list :

主要配置如下:

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=你好开发我的 map 在哪里?

最佳答案

默认情况下,当 Spring 遇到 Map<String, [type]> 类型的 Autowiring 字段时它将注入(inject)特定 [type] 的 beans 映射.在你的情况下 String .你不会得到你配置的 map 。

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

您基本上遇到了极端情况,因为您有一个以字符串作为键的映射。要得到你的 bean ,你要么必须放一个 @Qualifier("profileDependentProps")@Autowired 旁边或使用 @Resource("profileDependentProps")而不是 @Autowired .

关于java - Spring Profiles 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22301878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com