请教:Spring @Autowired注解实现了接口的成员变量时失败
场景:
1、类A中有成员变量类B,类B使用@Autowired注解的方式注入A中。
2、类B实现了接口IB.
问题:
启动时报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.siqisoft.test.B com.siqisoft.test.A.b;
nested exception is java.lang.IllegalArgumentException:
Can not set com.siqisoft.test.B field com.siqisoft.test.A.b to $Proxy23
疑问:
为什么实现了接口的B不能被注入到A中呢?
类A:
类B:
B实现的接口
1、类A中有成员变量类B,类B使用@Autowired注解的方式注入A中。
2、类B实现了接口IB.
问题:
启动时报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.siqisoft.test.B com.siqisoft.test.A.b;
nested exception is java.lang.IllegalArgumentException:
Can not set com.siqisoft.test.B field com.siqisoft.test.A.b to $Proxy23
疑问:
为什么实现了接口的B不能被注入到A中呢?
类A:
package com.siqisoft.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class A {
@Autowired
B b;
}
类B:
package com.siqisoft.test;
import org.springframework.stereotype.Component;
@Component
public class B {
public void BMethod(){};
}
B实现的接口
package com.siqisoft.test;
public abstract interface IB {
}
...全文
相关推荐
spring注解注入示例详解.pdf
spring框架的注解的介绍,有需要的可以下载看一下,不错的选择
因Spring AOP导致@Autowired依赖注入失败的解决方法
主要给大家介绍了因Spring AOP导致@Autowired依赖注入失败的解决方法,文中通过示例代码给大家介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
Spring @CrossOrigin 注解原理实现
主要介绍了Spring @CrossOrigin 注解原理实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring注解笔记
spring注解笔记spring注解笔记spring注解笔记spring注解笔记
解决Springboot @Autowired 无法注入问题
WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入。接下来给大家介绍解决Springboot @Autowired 无法注入问题,感兴趣的朋友一起看看吧


