在web开发中可以在web.xml文件中配置自启动的servlet或者web容器监听器,借助两者中的任何一者都可以完成启动spring web应用上下文的工作。
下面给出使用web监听器的方式来实现启动是spring容器的配置文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>demo-demo Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/spring/common/appcontext-*.xml,
classpath*:config/spring/local/appcontext-*.xml,
classpath*:config/spring/local/emidas-activity-biz-*.xml,
classpath*:config/spring/local/emidas-activity-web-*.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>cat-filter</filter-name>
<filter-class>com.dianping.cat.servlet.CatFilter</filter-class>
</filter>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml</param-value>
</init-param>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<!--<filter-class>com.dianping.decorator.sitemesh.ExtendedSiteMeshFilter</filter-class>-->
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>com.dianping.midas.authority.api.common.AuthorityFilter</filter-class>
<init-param>
<!-- <param-name>include</param-name>
<param-value>login,selectActivity,addActivityInit,updateActivity,addLaunchInit,selectActivity,updateActivity,selectModule,addModuleInit,updateModule</param-value> -->
<param-name>exclude</param-name>
<param-value>/getActivityDataSum,/activities/external/ajax,/penaltyChartClear,/activityChartClear,/penaltyChart,/activityChart,/mactivity/common,/brandArea,/getImgUrlAjax,/ftpUpLoadImgAjax,/index.html,/Heartbeat.html,/getPrizePackageUrl,/getLaunchInfoByActivityId,/addLaunchInterface,/freeOrderLaunchStatus,/invokeForMaterial,/checkToken,/listShopsAjaxNew,/listShopsAjax,/generationLaunch,/pushActivity,/promotion,/operation,/img,/js,/css,/newClickdate</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>cat-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>JspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>errorHandlerServlet</servlet-name>
<servlet-class>com.dianping.emidas.base.errorhandle.ErrorHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>errorHandlerServlet</servlet-name>
<url-pattern>/errorHandlerServlet.xfc</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorHandlerServlet.xfc</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorHandlerServlet.xfc</location>
</error-page>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>com.dianping.cat.servlet.CatListener</listener-class>
</listener>
<strong><listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></strong>
<listener>
<listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>
<servlet>
<servlet-name>sitemesh-freemarker</servlet-name>
<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.ftl</welcome-file>
</welcome-file-list>
</web-app></span>
因为我们使用的是web监听器的方式来进行的,所以必须将
Log4jConfigListener放在
ContextLoaderListener的前面,
采用上面的配置方式spring将自动使用XmlWebApplicationContext启动spring容器,即通过XML文件为spring容器提供bean的配置信息。