listener与load-on-startup

本文探讨了在Web应用启动过程中,Listener与Load-on-startup的区别与联系。Listener相较于Load-on-startup拥有更高的优先级,并且从Servlet 2.3版本开始被定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

春节期间在看一些源码时,在XML文件中看到两个标签<listner>和<load-on-startup>,嗯?自己开始有些想法,它们不都是在Web应用启动时,启动某些后台程序,负责为系统运行提供支持。既然作用类似,区别又在什么地方呢?
赶紧查阅资料,以下是对资料的摘录:
1、listener 比 load-on-startup优先加载
2、listtener在Servlet 2.3中才定义
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>metadata-complete</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>exampleServlet</servlet-name> <servlet-class>com.example.ExampleServlet</servlet-class> </servlet> <servlet> <description>generated-servlet</description> <servlet-name>KuCun2 Servlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:KuCun2-web-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <description>generated-resources-servlet</description> <servlet-name>Resource Servlet</servlet-name> <servlet-class> org.springframework.js.resource.ResourceServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>exampleServlet</servlet-name> <url-pattern>/example</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Resource Servlet</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
最新发布
06-21
<?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" xmlns:web="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_3_0.xsd" version="3.0"> <display-name>web application framework</display-name> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/waf</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <session-config> <session-timeout>60</session-timeout> <cookie-config><name>JSESSIONID</name></cookie-config> </session-config> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring_context.xml ,classpath:quartz_context.xml ,classpath:spring_context_misc.xml ,classpath:bpms_context.xml, classpath:process_group.xml </param-value> </context-param> <listener> <listener-class>cn.ccccltd.waf.listener.SessionListener</listener-class> </listener> <listener> <listener-class>cn.ccccltd.waf.listener.CacheFileEvent</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class> </listener> <listener> <listener-class>cn.ccccltd.bpms.BpmsInitializer</listener-class> </listener> <listener> <listener-class>cn.ccccltd.bpms.ou.listener.BpmsSessionListener</listener-class> </listener> <filter> <filter-name>SigninFilter</filter-name> <filter-class>cn.ccccltd.waf.SigninFilter</filter-class> <init-param> <param-name>signinUrlPattern</param-name> <param-value>bo,dhtml,dhtm,shtml,shtm</param-value> </init-param> </filter> <filter-mapping> <filter-name>SigninFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ****************************************************************************** --> <filter> <filter-name>xssFilter</filter-name> <filter-class>cn.ccccltd.waf.filter.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>xssFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>SSOLoginFilter</filter-name> <filter-class>cn.ccccltd.waf.sso.controller.SSORequestController</filter-class> </filter> <filter-mapping> <filter-name>SSOLoginFilter</filter-name> <url-pattern>*.bo</url-pattern> </filter-mapping> <!--<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>--> <servlet> <servlet-name>appDispatcher</servlet-name> <servlet-class>cn.ccccltd.waf.AppDispatcher</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring_dispatcher_context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC 请求映射 --> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.bo</url-pattern> </servlet-mapping> <servlet> <servlet-name>appLoader</servlet-name> <servlet-class>cn.ccccltd.waf.AppLoader</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet> <servlet-name>monitorLoader</servlet-name> <servlet-class>cn.ccccltd.waf.MonitorLoader</servlet-class> <load-on-startup>30</load-on-startup> </servlet> <servlet> <servlet-name>fileUpload</servlet-name> <servlet-class>cn.ccccltd.waf.tool.FileUpload</servlet-class> <load-on-startup>97</load-on-startup> </servlet> <servlet> <servlet-name>randomCode</servlet-name> <servlet-class>cn.ccccltd.waf.tool.RandomCode</servlet-class> <load-on-startup>99</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.dhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.dhtm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.shtm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>fileUpload</servlet-name> <url-pattern>/waf/tool/fileUpload</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>randomCode</servlet-name> <url-pattern>/waf/tool/randomCode</url-pattern> </servlet-mapping> <servlet> <servlet-name>FileUploadServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.FileUploadServlet</servlet-class> <init-param> <param-name>fileSizeLimit</param-name> <param-value>2000</param-value> </init-param> </servlet> <servlet> <servlet-name>DeleteFileServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.DeleteFileServlet</servlet-class> </servlet> <servlet> <servlet-name>FileDownloadServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.FileDownloadServlet</servlet-class> </servlet> <!-- 帆软报表 --> <!-- <servlet> <servlet-name>ReportServer</servlet-name> <servlet-class>com.fr.web.ReportServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ReportServer</servlet-name> <url-pattern>/ReportServer</url-pattern> </servlet-mapping> --> <servlet-mapping> <servlet-name>FileDownloadServlet</servlet-name> <url-pattern>/FileDownloadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FileUploadServlet</servlet-name> <url-pattern>/FileUploadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DeleteFileServlet</servlet-name> <url-pattern>/DeleteFileServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- error-page START --> <!-- 400 --> <error-page> <error-code>400</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 403 --> <error-page> <error-code>403</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 404 --> <error-page> <error-code>404</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 500 --> <error-page> <error-code>500</error-code> <location>/errorPage.jsp</location> </error-page> <!-- error-page END --> </web-app>那你给我修改一下文件都在项目之中。
06-11
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.3" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 配置spring资源 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <!-- 乱码问题 --> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置springmvc DispatcherServlet接收到的http请求通过DispatcherServlet进行分发。--> <servlet> <servlet-name>note</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--Sources标注的文件夹下需要新建一个spring文件夹--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>note</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>检查代码修改正确
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值