gpt4 book ai didi

java - 如果设置@Enableconfigurationproperties,为什么@Contexthierarchy不缓存bean?

转载 作者:行者123 更新时间:2023-11-30 06:16:50 25 4
gpt4 key购买 nike

我决定编写一个没有“@SpringBootTest”的“组件”测试。如果设置@Enableconfigurationproperties,@Сontexthierarchy 不会缓存bean。

当我一起运行“TestOne”和“TestTwo”时,HelloWorld 组件被初始化两次,“init666”中字符串的两次出现就证明了这一点。可能是什么问题?

测试一

@RunWith(SpringRunner.class)
@EnableConfigurationProperties
@ContextHierarchy({
@ContextConfiguration(classes = TestConfiguration.class),
@ContextConfiguration(classes = TestOneConfiguration.class)
})
public class TestOne {

@Autowired
HelloWorld helloWorld;

@Test
public void test () {

}

}

TestOne配置

@Configuration
public class TestOneConfiguration {
}

测试二

@RunWith(SpringRunner.class)
@EnableConfigurationProperties
@ContextHierarchy({
@ContextConfiguration(classes = TestConfiguration.class),
@ContextConfiguration(classes = TestTwoConfiguration.class)
})
public class TestTwo {

@Autowired
HelloWorld helloWorld;

@Test
public void test () {

}
}

TestTwo配置

@Configuration
public class TestTwoConfiguration {
}

测试配置

@Configuration
public class TestConfiguration {

@Bean
HelloWorld helloWorld () {
return new HelloWorld();
}

HelloWorld

@Component

public class HelloWorld {
public HelloWorld() {
System.out.println("init666");
}
}

屏幕截图:double appearance of the string in "init666"

附注@SpringBootTest无法使用

最佳答案

目前我找到了唯一的解决方案:

摆脱@Enableconfigurationproperties

从 application.yml 加载属性

TestOne

@RunWith(SpringRunner.class)
@EnableConfigurationProperties
@ContextHierarchy({
@ContextConfiguration(classes = TestConfiguration.class,
initializers = TestContextInitializer.class),
@ContextConfiguration(classes = TestOneConfiguration.class)
})

public class TestOne {

@Autowired
HelloWorld helloWorld;

@Test
public void test () {

}
}

TestContextInitializer

public class TestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:application.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> yamlTestProperties = sourceLoader.load("applicationProperties", resource, null);
applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties);
String[] profiles = applicationContext.getEnvironment().getProperty("spring.profiles.active").replaceAll(" ", "").split(",");
applicationContext.getEnvironment().setActiveProfiles(profiles);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

通过Enviroment雕刻所需字段

@Autowired
private Environment environment;

public Someclass somemethod() {
Someclass someclass = new Someclass();
String someField = environment.getProperty("someField");
someclass.setSomeField(someField);
return someclass;
}

如果有更好的东西,我会很高兴看到这些建议。
谢谢!

关于java - 如果设置@Enableconfigurationproperties,为什么@Contexthierarchy不缓存bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49014628/

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