【报错】
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh’ before accessing beans via the ApplicationContext
【报错原因及解决方法】
1. 没有指定配置文件
Spring实例化BeanFactory的时候是默认到classPath下面查找名为applicationContext.xml的文件的
第一种方法:
xml文件在包内
String xmlPath="com/itheima/jdbc/applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
第二种方法:
xml文件与包并排
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
2. spring初始化bean对象出错;
1)xml的bean的id 有重复;
2)如果是注解配置:可能是注解的名称有重复,只需要将重复的bean 的id和重复的注解改了就行了;