ApplicationContext getBean 的注意点

tech2026-09-24  2

getBean(Class) 传入一个 Class(无论是类、抽象类、接口),只要传入的 Class 在容器中还存在它的子类,就会报错:

因为 Spring 无法准确返回给你到底是本类,还是子类,除非使用 getBean(name, requiredType),同时指定 name 限制获取

 

最新回复(0)