代码之家  ›  专栏  ›  技术社区  ›  Luca

弹簧@自动连线对象为空

  •  2
  • Luca  · 技术社区  · 7 年前

        @ContextConfiguration(classes = [MyServiceImplementation.class])
        class MyServiceSpecification extends Specification {
    
             @Autowired
             private MyService myServiceImplementation
    
             def "  " {
                 //code
             }
        }
    

    班级 MyServiceImplementation @Service . 我没有使用XML配置。 MyServiceImpl MyService .

    myServiceImplementation 无效的

    ComponentScan 但它仍然不起作用。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Leonard Brünings    7 年前

    首先,你需要两者兼得 spock-core spock-spring 在类路径上。第二 @ContextConfiguration(classes=

    @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();
        }
    }