做网站鼠标移动 链接变颜色,医院网站建设的要求,html5网站模板,找做仿网站在项目开发过程中#xff0c;往往需要一些功能随着项目启动而优先启动#xff0c;下面我总结几种方式#xff08;非spring boot#xff09; spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) 方式一#xff1a; ServletContextListener监听器#…在项目开发过程中往往需要一些功能随着项目启动而优先启动下面我总结几种方式非spring boot spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) 方式一 ServletContextListener监听器不懂监听器的可以去网上百度一下servlet的监听器 java 代码如下
package com.deifeng.huhy.common.run;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class RunStartOne implements ServletContextListener {private static final Logger LOGGER LoggerFactory.getLogger(RunStartOne.class);public RunStartOne() {}public void contextDestroyed(ServletContextEvent arg0) {}public void contextInitialized(ServletContextEvent arg0) {try {// 需要实现的功能System.out.println(随项目启动方式一----------------》);} catch (Exception e) {LOGGER.error(GreyClientInitListener error, e);}}}web.xml配置如下 listenerlistener-classcom.deifeng.huhy.common.run.RunStartOne/listener-class/listener 项目启动如下 方式二 ApplicationListener 代码实现如下
package com.deifeng.huhy.common.run;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;Service
public class RunStartTwo implements ApplicationListenerContextRefreshedEvent {private static final Log LOGGER LogFactory.getLog(RunStartTwo.class);Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {try {// 在web项目中spring mvc系统会存在两个容器一个是root application context// ,另一个就是我们自己的 projectName-servlet context作为root application context的子容器。// 这种情况下就会造成onApplicationEvent方法被执行两次。为了避免这个问题我们可以只在root// application context初始化完成后调用逻辑代码其他的容器的初始化完成则不做任何处理。if (event.getApplicationContext().getParent() null) {// 需要实现的功能System.out.println(随项目启动方式二----------------》);}} catch (Exception e) {LOGGER.error(StartGateServiceData, e);}}
}启动成功如下 方式三extends HttpServlet 代码实现public class RunStartThree extends HttpServlet {// Servlet的init方法会在Tomcat启动的时候执行public void init() throws ServletException {// 需要实现的功能System.out.println(随项目启动方式三--------------》);}
}web.xml的配置servletservlet-nameinit/servlet-nameservlet-classcom.deifeng.huhy.common.run.RunStartThree/servlet-classload-on-startup1/load-on-startup/servlet注意 有人会问为啥不配servlet-map 简单解释一下这个功能是随着项目启动而启动我不允许有url能访问到所以在这就没配置如果你配上也没问题不会保错为了代码的安全性我就不配做url的映射了启动项目成功如图 到这还没结束不知道有没有人会思考这三种启动方式有没有先后啊现在代码都有了我就在这测一下------------------------------------ 现在我把三种方式都放开启动项目我们看日志记录就行了 applicaton listener httpServlet 分析 这个优先顺序和servlet容器有关如果你研读过servlet的源码 1.web项目中spring mvc系统会存在两个容器一个是root application context。另一个就是我们自己的 projectName-servlet context作为root application context的子容器。这种情况下就会造成onApplicationEvent方法被执行两次。为了避免这个问题我们可以只在root application context初始化完成后调用逻辑代码其他的容器的初始化完成则不做任何处理。【对应于方式二】 2.系统容器是root在先 然后就是项目容器对于servelt的加载顺序 监听器》过滤器 》servlet 的加载机制所以造成上面那张启动顺序有时间可以看看servlet源码 转载于:https://www.cnblogs.com/huhongy/p/8342203.html