场景:
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:
- Java code
package com.siqisoft.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class A { @Autowired B b;}
类B:
- Java code
package com.siqisoft.test;import org.springframework.stereotype.Component;@Componentpublic class B { public void BMethod(){};}
B实现的接口
- Java code
package com.siqisoft.test;public abstract interface IB { }
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
Error creating bean with name 'a':
又那里来的a列?
关于你的思考,回答如下:
用
@Autowired
@Qualifier("b") //用B类型去注入. 请注意类名首字母小写
private BI b