1、因为spring要建立属于自己的容器,就必须要加载自己的配置文件。
这个时候,需要注册ContextLoaderListener或者这个类的子类。
在web.xml加上以下的信息:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
当然,这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中
添加如下信息。可以参考我的示例,指定配置文件,如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/app-context.xml</param-value>
</context-param>
注意:<param-name>contextConfigLocation</param-name>是不能改变的。<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/app-context.xml</param-value>
</context-param>