springboot测试类Error creating bean with name

超、凢脫俗 2023-10-05 17:30 168阅读 0赞

今天用测试类的时候报错:
font color=red>Error creating bean with name xxx

原因: 我把@Service加在了接口上,其实应该加在实现类上。

@Service注解是标注在实现类上的,因为@Service是把spring容器中的bean进行实例化,也就是等同于new操作,只有实现类是可以进行new实例化的,而接口则不能,所以是加在实现类上的。

另外要记得引入,否则会报Failed to load ApplicationContext

  1. <!-- Junit依赖 -->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <scope>test</scope>
  6. </dependency>

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读