BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
SSM整合出现上述错误有以下两个原因 :
1. Spring 的配置文件中有语法错误
2. 找不着Spring 的配置文件
Spring的配置文件默认名称和地址是 /WEB-INF/applicationContext.xml
,所以如果我们用的是自定义的名称和地址(如:放在类路径下叫 spring.xml
), 那么就需要我们在web.xml
中指定它的位置和名称:
<!--spring的核心配置文件加载-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
上面 xml 中的 param-name
是 ContextLoaderListener
的一个属性名称,所以名称固定,不能写错;
param-value
包含了两个信息, classpath:
指定了配置文件的路径, spring.xml
指定了配置文件的名称.