我正在 spock 框架中编写规范类。
@ContextConfiguration(classes = [MyServiceImplementation.class])
class MyServiceSpecification extends Specification {
@Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
MyServiceImplementation
类被注释为@Service
。我没有使用 XML 配置。 MyServiceImpl
是接口(interface)的实现:MyService
。
为什么 Autowiring 对象 myServiceImplementation
为空?
我尝试使用 ComponentScan
,但它仍然无法正常工作。
最佳答案
首先,您需要在类路径中同时拥有 spock-core
和 spock-spring
。其次,@ContextConfiguration(classes=
采用配置类列表,而不是 bean 类。
@ContextConfiguration(classes = [MyConfig.class])
class MyServiceSpecification extends Specification {
@Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
// You could also define @ComponentScan here
class MyConfig {
@Bean
MyService myService() {
return new MyServiceImplementation();
}
}
关于Spring @Autowired 对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281022/