ApplicationContext applicationContext= new ClassPathXmlApplicationContext("bean.xml");
Object object = applicationContext.getBean("mybeanId");
将这两段代码封装到自己写的BeanFactory类里变成静态方法:
正确代码:
public class BeanFactory {
private static ApplicationContext ac;
static{
ac = new ClassPathXmlApplicationContext("bean.xml");
}
public static Object getBean(String beanId){
return ac.getBean(beanId);
}
}
犯错原因:
误把BeanFactory类中getBean方法中的
return ac.getBean(beanId);写成了return ac.getBean("beanId");
改正做法:
去掉双引号。
Spring学习过程中遇到的No bean named 'beanId' is defined报错
原文:https://www.cnblogs.com/WuLang123/p/14383616.html