当前位置: 首页 > news >正文

网站排名怎么上去重庆网站seo教程

网站排名怎么上去,重庆网站seo教程,做律师网站的公司,go 网站开发SpringMVC的启动入口在SpringServletContainerInitializer类,它是ServletContainerInitializer实现类(Servlet3.0新特性)。在实现方法中使用WebApplicationInitializer创建ApplicationContext、创建注册DispatcherServlet、初始化ApplicationContext等。 SpringMVC…

SpringMVC的启动入口在SpringServletContainerInitializer类,它是ServletContainerInitializer实现类(Servlet3.0新特性)。在实现方法中使用WebApplicationInitializer创建ApplicationContext、创建注册DispatcherServlet、初始化ApplicationContext等。

SpringMVC已经将大部分的启动逻辑封装在了几个抽象WebApplicationInitializer中,开发者只要继承这些抽象类实现抽象方法即可。

本文将详细分析ServletContainerInitializer、SpringServletContainerInitializer和WebApplicationInitializer的工作流程。

Servlet3.0的ServletContainerInitializer

ServletContainerInitializer接口

ServletContainerInitializer是Servlet3.0的接口。

该接口在web应用程序启动阶段接收通知,注册servlet、filter、listener等。

该接口的实现类可以用HandlesTypes进行标注,并指定一个Class值,后续会将实现、扩展了这个Class的类集合作为参数传递给onStartup方法。

以tomcat为例,在容器配置初始化阶段,将使用SPI查找实现类,在ServletContext启动阶段,初始化并调用onStartup方法来进行ServletContext的初始化。

SpringServletContainerInitializer实现类

在onStartup方法创建所有的WebApplicationInitializer对并调用onStartup方法。

以下为SPI配置,在spring-web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer文件:

在这里插入图片描述

WebApplicationInitializer接口

WebApplicationInitializer接口概述

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically – as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.

Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. See its Javadoc for details on this bootstrapping mechanism.

示例:

public class MyWebAppInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext container) {// Create the 'root' Spring application contextAnnotationConfigWebApplicationContext rootContext =new AnnotationConfigWebApplicationContext();rootContext.register(AppConfig.class);// Manage the lifecycle of the root application contextcontainer.addListener(new ContextLoaderListener(rootContext));// Create the dispatcher servlet's Spring application contextAnnotationConfigWebApplicationContext dispatcherContext =new AnnotationConfigWebApplicationContext();dispatcherContext.register(DispatcherConfig.class);// Register and map the dispatcher servletServletRegistration.Dynamic dispatcher =container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));dispatcher.setLoadOnStartup(1);dispatcher.addMapping("/");}
}

开发者可以编写类继承AbstractAnnotationConfigDispatcherServletInitializer抽象类,这个抽象类已经将大部分的初始化逻辑进行了封装。

WebApplicationInitializer实现和继承关系

在这里插入图片描述

AbstractContextLoaderInitializer类:

Convenient base class for WebApplicationInitializer implementations that register a ContextLoaderListener in the servlet context. The only method required to be implemented by subclasses is createRootApplicationContext(), which gets invoked from registerContextLoaderListener(ServletContext).

AbstractDispatcherServletInitializer类:

Base class for WebApplicationInitializer implementations that register a DispatcherServlet in the servlet context. Most applications should consider extending the Spring Java config subclass AbstractAnnotationConfigDispatcherServletInitializer.

AbstractAnnotationConfigDispatcherServletInitializer类:

WebApplicationInitializer to register a DispatcherServlet and use Java-based Spring configuration.
Implementations are required to implement:

  • getRootConfigClasses() – for “root” application context (non-web infrastructure) configuration.
  • getServletConfigClasses() – for DispatcherServlet application context (Spring MVC infrastructure) configuration.

If an application context hierarchy is not required, applications may return all configuration via getRootConfigClasses() and return null from getServletConfigClasses().

开发者SpringMvcInitializer示例

开发者需要编写类继承AbstractAnnotationConfigDispatcherServletInitializer类,实现几个抽象方法:

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {/*** 指定创建root application context需要的@Configuration/@Component类*/@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{AppConfig.class};}/*** 指定创建Servlet application context需要的@Configuration/@Component类* 如果所有的配置类都使用root config classes就返回null*/@Overrideprotected Class<?>[] getServletConfigClasses() {return null;}/*** Specify the servlet mapping(s) for the DispatcherServlet — for example "/", "/app", etc.*/@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}
}

SpringMVC启动流程

入口AbstractDispatcherServletInitializer.onStartup方法

public void onStartup(ServletContext servletContext) throws ServletException {super.onStartup(servletContext);registerDispatcherServlet(servletContext);
}

父类的onStartup方法创建RootApplicationContext、注册ContextLoaderListener监听器:

public void onStartup(ServletContext servletContext) throws ServletException {registerContextLoaderListener(servletContext);
}protected void registerContextLoaderListener(ServletContext servletContext) {WebApplicationContext rootAppContext = createRootApplicationContext();if (rootAppContext != null) {// ContextLoaderListener是ServletContextListener// 会在contextInitialized阶段初始化RootApplicationContextContextLoaderListener listener = new ContextLoaderListener(rootAppContext);listener.setContextInitializers(getRootApplicationContextInitializers());servletContext.addListener(listener);}
}

注册DispatcherServlet

registerDispatcherServlet方法用于创建ServletApplicationContext、注册DispatcherServlet:

protected void registerDispatcherServlet(ServletContext servletContext) {String servletName = getServletName();// 创建ServletApplicationContextWebApplicationContext servletAppContext = createServletApplicationContext();// 创建DispatcherServletFrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);// 添加ApplicationContextInitializer集,会在初始化时调用dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());// 添加到ServletContextServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);registration.setLoadOnStartup(1);registration.addMapping(getServletMappings());registration.setAsyncSupported(isAsyncSupported());Filter[] filters = getServletFilters();if (!ObjectUtils.isEmpty(filters)) {for (Filter filter : filters) {registerServletFilter(servletContext, filter);}}customizeRegistration(registration);
}

触发ContextLoaderListener监听器contextInitialized事件

这个是Servlet的ServletContextListener机制,在ServletContext创建之后触发contextInitialized事件:

public void contextInitialized(ServletContextEvent event) {initWebApplicationContext(event.getServletContext());
}public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {// 判断是否已经在当前ServletContext绑定了WebApplicationContext// 如果已经绑定抛错if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {throw new IllegalStateException("已经初始化过了");}try {if (this.context == null) {this.context = createWebApplicationContext(servletContext);}if (this.context instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;if (!cwac.isActive()) {if (cwac.getParent() == null) {ApplicationContext parent = loadParentContext(servletContext);cwac.setParent(parent);}// refreshconfigureAndRefreshWebApplicationContext(cwac, servletContext);}}servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);ClassLoader ccl = Thread.currentThread().getContextClassLoader();if (ccl == ContextLoader.class.getClassLoader()) {currentContext = this.context;} else if (ccl != null) {currentContextPerThread.put(ccl, this.context);}return this.context;} catch (RuntimeException | Error ex) {servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);throw ex;}
}protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {// 给wac设置idwac.setServletContext(sc);// 设置spring主配置文件路径String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);if (configLocationParam != null) {wac.setConfigLocation(configLocationParam);}ConfigurableEnvironment env = wac.getEnvironment();if (env instanceof ConfigurableWebEnvironment) {((ConfigurableWebEnvironment) env).initPropertySources(sc, null);}customizeContext(sc, wac);// 刷新ApplicationContextwac.refresh();
}

触发DispatcherServlet的init事件

Servlet在接收请求之前会调用其init方法进行初始化,这个是Servlet的规范。

init方法在其父类HttpServletBean中:

public final void init() throws ServletException {// 从ServletConfig获取配置设置到ServletPropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);if (!pvs.isEmpty()) {try {BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));initBeanWrapper(bw);bw.setPropertyValues(pvs, true);} catch (BeansException ex) {throw ex;}}// 初始化initServletBean();
}// FrameworkServlet
protected final void initServletBean() throws ServletException {try {// 初始化ServletApplicationContextthis.webApplicationContext = initWebApplicationContext();initFrameworkServlet();} catch (ServletException | RuntimeException ex) {throw ex;}
}protected WebApplicationContext initWebApplicationContext() {// 获取rootApplicationContext// 之前的ServletContext初始化阶段已经绑定WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext wac = null;if (this.webApplicationContext != null) {wac = this.webApplicationContext;if (wac instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;if (!cwac.isActive()) {// 将rootApplicationContext设置为Parentif (cwac.getParent() == null) {cwac.setParent(rootContext);}// 刷新ApplicationContextconfigureAndRefreshWebApplicationContext(cwac);}}}// 如果没有需要查找或创建if (wac == null) {wac = findWebApplicationContext();}if (wac == null) {wac = createWebApplicationContext(rootContext);}if (!this.refreshEventReceived) {synchronized (this.onRefreshMonitor) {// 子类实现onRefresh(wac);}}if (this.publishContext) {String attrName = getServletContextAttributeName();getServletContext().setAttribute(attrName, wac);}return wac;
}// DispatcherServlet
protected void onRefresh(ApplicationContext context) {initStrategies(context);
}// 初始化SpringMVC相关组件
protected void initStrategies(ApplicationContext context) {initMultipartResolver(context);initLocaleResolver(context);initThemeResolver(context);initHandlerMappings(context);initHandlerAdapters(context);initHandlerExceptionResolvers(context);initRequestToViewNameTranslator(context);initViewResolvers(context);initFlashMapManager(context);
}

SpringMVC启动流程总结

  • 创建RootApplicationContext、注册ContextLoaderListener监听器
  • 创建ServletApplicationContext、注册DispatcherServlet
  • 触发ContextLoaderListener监听器contextInitialized事件,初始化RootApplicationContext
  • 触发DispatcherServlet的init事件,初始化ServletApplicationContext
http://www.yayakq.cn/news/625049/

相关文章:

  • 宁波做网站哪里专业重庆市建设工程信息网招标文件
  • 软件工网站开发课程设计报告论坛网站制作模板
  • 网站建设盒子模型浮动企业网站优化问题
  • 西安建网站网站推广网络营销名词解释是什么
  • 好网站开发科技有限公司 翻译
  • 西安知名的集团门户网站建设公司企业外贸网站建设
  • asp 通过ftp 网站搬家论文网站建设
  • 网站建设公司主要品牌网站设计制作哪家正规
  • 网站怎样做排名wordpress 宝典 pdf
  • 做网站着用什么软件wordpress+视差模板
  • 什么网站做兼职最好怎样把字体导入wordpress
  • 微商货源网什么什么网站建设青岛公司网站设计
  • 焦作维科网站建设公司成都网页设计美工培训
  • 江苏省和住房城乡建设厅网站首页wordpress注册不了
  • 手机网站建设市场淘宝网页版电脑版官网
  • 免费招聘网站有哪些平台wordpress上传到哪个目录
  • 如何创建网站下载网站建设中图片
  • 长沙模板网站长沙网站建设晋城企业网站建设价格
  • 电子商务网站设计与网络营销实验谷歌云wordpress绑定域名
  • 白云区建网站海口网站建设方面
  • 嘉兴中元建设网站智卡会员管理系统
  • 做网站如何哪位大神给个网址
  • 卖自己做的网站去哪公众号平台有哪些
  • 可以做公司宣传的网站有哪些内容广东知名网站
  • 村级网站建站营销助手
  • 南昌公司网站建设模板北京创意设计协会网站
  • 怎么让公司网站显示官网设计用什么软件好
  • 如何建设公司网站 需要注意什么视频广告对接平台
  • 域名注册查询网站外置硬盘可以做网站访问
  • 如何免费做公司网站网站建设要不要学编码