请教:Spring @Autowired注解实现了接口的成员变量时失败

yu1ei yu1ei 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
场景:
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 {

}

...全文

相关推荐

评论 24
收藏 已收藏
分享
返回顶部
yu1ei
Java EE 67,535 成员 225,843 内容