Spring @Autowired对象为null

路卡

我在Spock框架中编写规范类。

    @ContextConfiguration(classes = [MyServiceImplementation.class])
    class MyServiceSpecification extends Specification {

         @Autowired
         private MyService myServiceImplementation

         def "  " {
             //code
         }
    }

该类MyServiceImplementation带有注释@Service我没有使用XML配置。MyServiceImpl是接口的实现:MyService

为什么自动装配对象为myServiceImplementation空?

我尝试使用ComponentScan,但仍然无法正常工作。

伦纳德·布鲁宁斯

首先,你需要有两个spock-corespock-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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring-Boot @Autowired变量为null

在UsernamePasswordAuthenticationFilter Spring Boot中@autowired为null

Autowired属性为null-Spring Boot配置

为什么Spring @Autowired ApplicationContext在集成测试中为null?

在Spring Boot应用程序中Autowired字段为null

Curl Spring复杂对象为null

Spring Boot JsonMappingException:对象为null

使用 spring 安全性时,Spring Boot @Autowired 存储库实例为 null

Spring @Autowired restTemplate为空

为什么即使我在Spring类中delcare字段,@ Autowired还是为null?

无法从Spring Boot的属性文件中加载密钥,@ Autowired为null

Spring Data JPA + OpenJPA @autowired存储库字段在webapp中为null,但对于junit则很好

当作为Spring bean的SubClass调用同时也是Spring bean的SuperClass的方法时,SuperClass中的autowired字段为null

如果为null,Java Spring不会返回DTO对象吗?

Spring @Autowired变量在@Component内部为空

Spring @Configuration 注解 @Autowired 服务为空

Spring @Autowired(required = true)为空

Spring + Hibernate @autowired在ConstraintValidator中为空

Spring Autowired null如果找不到

Autowired在CustomInterceptor中获取null(Spring Boot)

Spring @Autowired无法正常工作并返回null

spring @Autowired存储库返回null

具有Ehcache 3.x的Spring Boot 2.x-自定义CacheEventListener中的@Autowired服务为null

Autowired为空,不适用于Jersey + Spring

Spring Boot 2 @Transactional注释使Autowired字段为空

Spring-@Configuration中的@Autowired bean为空

为什么我的Spring @Autowired字段为空?

来自 Spring Boot 的 @Autowired 服务在 WAR 中为空

与Spring Hibernate的@Autowired冲突@Entity非列对象