web.xml中配置Spring中applicationContext.xml的方式

时间:2021-08-27 22:25:45
2011-11-08 16:29

web.xml中配置Spring中applicationContext.xml的方式

使用web.xml方式加载Spring时,获取Spring applicationContext.xml的方式

1、servlet方式加载:

【web.xml】

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
</servlet>

2、listener方式加载:

【web.xml】

在web.xml里配置Listener

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

如果在web.xml里给该Listener指定要加载的xml,如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。

但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。

注意:红色部分是固定的不能改变。contextConfigLocation就是用来加载applicationContext.xml的固定名,不能改变。

加载src下的xml文件

classpath:applicationContext.xml

加载/WEB-INF/下的xml文件

/WEB-INF/applicationContext.xml