- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我决定编写一个没有“@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/
我的意思是,如果我用它注释不相关的类,Spring会推断逻辑层次结构还是它是一种仅适合物理层次结构的机制(抽象类和子类,如Spring文档的示例) 我所追求的是一种告诉上下文缓存重用不符合套件的不同测
我们有使用元注释的测试类: @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(locations = {"/web/
我想在 SpringBootTest 中使用数据库缓存上下文。 当在 ContextHierarchy 中尝试两个 ContextConfiguration 时: @SpringBootTest(pr
我是一名优秀的程序员,十分优秀!