通過web.xml檔案自動啟動spring容器載入對應的配置檔案
阿新 • • 發佈:2019-01-22
在web開發中可以在web.xml檔案中配置自啟動的servlet或者web容器監聽器,藉助兩者中的任何一者都可以完成啟動spring web應用上下文的工作。
下面給出使用web監聽器的方式來實現啟動是spring容器的配置檔案
因為我們使用的是web監聽器的方式來進行的,所以必須將Log4jConfigListener放在ContextLoaderListener的前面,採用上面的配置方式spring將自動使用XmlWebApplicationContext啟動spring容器,即通過XML檔案為spring容器提供bean的配置資訊。<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>