启动报错:beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxService' is expected to be of t

启动报错:beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxService' is expected to be of type 'com.inter.IxxService' but was actually of type 'com.service.xxService$$EnhancerBySpringCGLIB$$8e6810a7'

spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

  @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了

也就是说:

 

启动报错:beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxService' is expected to be of t_第1张图片

使用@Resource  我们想注入的是ISysCouponService,随便定义了一个变量名称为 couponService , 而恰巧代码里又出现以couponService命名的类,并不是我们想要注入的bean,此时启动会报错,也就是说代码中如果没有这个命名的类什么事都没有,这只是一个巧合,所以说命名尽量规范。

 

使用 @Autowired 不会出现以上问题

 

 

你可能感兴趣的:(java,spring)