标题翻译
Validate spring context without refreshing it
问题
假设我有一个尚未刷新的Spring AnnotationConfigApplicationContext
实例,就像这样:
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(Config1.class);
context.register(Config2.class);
context.register(Config3.class);
context.register(Config4.class);
// ...
}
在单元测试期间是否有一种(运行时)方法来“验证”它 - 也就是说,确保所有bean 可以按照预期从Spring的角度进行连接,而无需刷新它?也就是说,确保没有丢失或重复的bean,拼写错误的属性,循环依赖等,而实际上并不实例化任何bean。
本质上,我正在寻找一种方法,至少在逻辑上,使所有bean定义都被模拟,即一些等效于:
@Bean
public SomeBean1 someBean1(SomeBean2 someBean2) {
return mock(SomeBean1.class);
}
@Bean
public SomeBean2 someBean2(@Value("${someProperty}") someProperty) {
return mock(SomeBean2.class);
}
尽管我不一定需要以上述方式完成。
英文翻译
Say I have a Spring AnnotationConfigApplicationContext
instance that was not refreshed yet, such as:
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(Config1.class);
context.register(Config2.class);
context.register(Config3.class);
context.register(Config4.class);
// ...
}
Is there a (runtime during a unit test) way to "validate" it - i.e. make sure all beans can be wired as expected from Spring's perspective at least - without refreshing it? That is, make sure there are no missing or duplicate beans, misspelled properties, circular dependencies, etc. without actually instantiating any of the beans?
In essence, I'm looking for a way to - at least logically - make all bean definitions be mocked, i.e. some equivalent of:
@Bean
public SomeBean1 someBean1(SomeBean2 someBean2) {
return mock(SomeBean1.class);
}
@Bean
public SomeBean2 someBean2(@Value("${someProperty}") someProperty) {
return mock(SomeBean2.class);
}
though I don't necessarily need it done in the above way obviously.
答案1
得分: 0
不。由于我需要再多17个字符,所以长回答也是:不。
英文翻译
Short answer: No. As I need 17 more characters, the long answer is also: No.
专注分享java语言的经验与见解,让所有开发者获益!
评论