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

做网站市场价格霸榜seo

做网站市场价格,霸榜seo,专门做颜料的网站,东莞企业网站建设报价Web上下文初始化 web上下文与SerlvetContext的生命周期应该是相同的&#xff0c;springmvc中的web上下文初始化是由ContextLoaderListener来启动的 web上下文初始化流程 在web.xml中配置ContextLoaderListener <listener> <listener-class>org.springframework.…

Web上下文初始化

web上下文与SerlvetContext的生命周期应该是相同的,springmvc中的web上下文初始化是由ContextLoaderListener来启动的

web上下文初始化流程
web上下文初始化流程

在web.xml中配置ContextLoaderListener

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
</context-param>

ContextLoaderListener

ContextLoaderListener实现了ServletContextListener接口,ServletContextListener是Servlet定义的,提供了与Servlet生命周期结合的回调,contextInitialized方法和contextDestroyed方法;ContextLoaderListener继承了ContextLoader类,通过ContextLoader来完成实际的WebApplicationContext的初始化工作,并将WebApplicationContext存放至ServletContext中,ContextLoader就像是spring应用在web容器中的启动器

从spring3.x起就不需要配置监听器ContextLoaderListener,只需要配置DispatcherServlet就可以了,只是没有父容器了而已,只读取mvc配置文件。如果想要spring父容器时,才需要配置ContextLoaderListener

可参考文章 spring与springmvc整合

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

   /**
    * Initialize the root web application context.
    */

  // ServletContext被创建时触发,初始化web上下文
   @Override
   public void contextInitialized(ServletContextEvent event) {
      initWebApplicationContext(event.getServletContext());
   }

   /**
    * Close the root web application context.
    */

  // ServletContext被销毁时触发,关闭web上下文
   @Override
   public void contextDestroyed(ServletContextEvent event) {
      closeWebApplicationContext(event.getServletContext());
      ContextCleanupListener.cleanupAttributes(event.getServletContext());
   }

}

在启动时会创建一个WebApplicationContext作为IOC容器,默认的实现类是XmlWebApplicationContext

/** Default config location for the root context */
// 默认的配置位置,如果不进行配置,就会读取这个文件
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

/** Default prefix for building a config location for a namespace */
// 配置文件默认在/WEB-INF/目录下
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";

/** Default suffix for building a config location for a namespace */
// 配置文件默认后缀名是.xml
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
initWebApplicationContext初始化
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
  // WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE是WebApplicationContext.class.getName() + ".ROOT"
  // 如果servletContext中已经存在根上下文属性,则说明已经有一个根上下文存在了
   if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
      throw new IllegalStateException(
            "Cannot initialize context because there is already a root application context present - " +
            "check whether you have multiple ContextLoader* definitions in your web.xml!");
   }

   Log logger = LogFactory.getLog(ContextLoader.class);
   servletContext.log("Initializing Spring root WebApplicationContext");
   
   long startTime = System.currentTimeMillis();

   try {
      // Store context in local instance variable, to guarantee that
      // it is available on ServletContext shutdown.
      if (this.context == null) {
        // 初始化context
         this.context = createWebApplicationContext(servletContext);
      }
      if (this.context instanceof ConfigurableWebApplicationContext) {
         ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
         if (!cwac.isActive()) {
            // The context has not yet been refreshed -> provide services such as
            // setting the parent context, setting the application context id, etc
            if (cwac.getParent() == null) {
               // The context instance was injected without an explicit parent ->
               // determine parent for root web application context, if any.
               ApplicationContext parent = loadParentContext(servletContext);
               cwac.setParent(parent);
            }
            configureAndRefreshWebApplicationContext(cwac, servletContext);
         }
      }
     // 记录在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 ex) {
      logger.error("Context initialization failed", ex);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
      throw ex;
   }
   catch (Error err) {
      logger.error("Context initialization failed", err);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
      throw err;
   }
}
创建WebApplicationContext上下文
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
  // 如果在web.xml中有配置contextClass,则使用配置的类
  // 如果没有配置,则使用默认的配置 ContextLoader.properties
  // org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

   Class<?> contextClass = determineContextClass(sc);
   if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
      throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
            "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
   }
   return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}

servlet3.0规范

servlet3.0后支持不使用web.xml的方式配置。

在spring-web中services下配置有javax.servlet.ServletContainerInitializer文件,其内容为org.springframework.web.SpringServletContainerInitializer,找到该类发现Servlet容器(例如tomcat)启动时,会将SPI注册的Java EE接口ServletContainerInitializer的所有的实现类(例如,SpringServletContainerInitializer)挨个回调其onStartup方法。

而onStartup方法是需要参数的,这时@HandlesTypes就派上用场了。onStartup方法所需要的参数就通过@HandlesTypes注解传入

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer

该类会调用所有WebApplicationInitializer的onStartup方法

for (WebApplicationInitializer initializer : initializers) {
   initializer.onStartup(servletContext);
}

找到WebApplicationInitializer的子类AbstractAnnotationConfigDispatcherServletInitializer,我们需要继承AbstractAnnotationConfigDispatcherServletInitializer实现其三个抽象方法

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

 /*
  * 获取根容器的配置类,该配置类就类似于我们以前经常写的Spring的配置文件,然后创建出一个父容器
  * 带有@Configuration注解的类用来配置ContextLoaderListener
  */

 @Override
 protected Class<?>[] getRootConfigClasses() {
  // TODO Auto-generated method stub
  return null;
 }

 /*
  * 获取web容器的配置类,该配置类就类似于我们以前经常写的Spring MVC的配置文件,然后创建出一个子容器
  * 带有@Configuration注解的类用来配置DispatcherServlet
  */

 @Override
 protected Class<?>[] getServletConfigClasses() {
  // TODO Auto-generated method stub
  return null;
 }

 // 获取DispatcherServlet的映射信息
 @Override
 protected String[] getServletMappings() {
  // TODO Auto-generated method stub
  return new String[]{"/"};
 }

}

这样我们就不需要配置web.xml配置文件了,其会同时创建DispatcherServlet和ContextLoaderListener

https://zhhll.icu/2020/框架/springmvc/底层剖析/0.Web上下文初始化/

本文由 mdnice 多平台发布


文章转载自:
http://qi.gcqs.cn
http://metronomic.gcqs.cn
http://obliger.gcqs.cn
http://cushaw.gcqs.cn
http://freesia.gcqs.cn
http://appurtenances.gcqs.cn
http://glutaminase.gcqs.cn
http://unwearable.gcqs.cn
http://cgh.gcqs.cn
http://bate.gcqs.cn
http://contend.gcqs.cn
http://alkylate.gcqs.cn
http://horsepox.gcqs.cn
http://subcelestial.gcqs.cn
http://commandership.gcqs.cn
http://drone.gcqs.cn
http://trapezium.gcqs.cn
http://nupe.gcqs.cn
http://pomeranchuk.gcqs.cn
http://seismologist.gcqs.cn
http://graphomaniac.gcqs.cn
http://sirenian.gcqs.cn
http://hedgerow.gcqs.cn
http://pseudocyesis.gcqs.cn
http://hooked.gcqs.cn
http://marge.gcqs.cn
http://ligneous.gcqs.cn
http://photoconduction.gcqs.cn
http://panelling.gcqs.cn
http://psychiatry.gcqs.cn
http://duressor.gcqs.cn
http://overstriking.gcqs.cn
http://ulcerogenic.gcqs.cn
http://freemasonry.gcqs.cn
http://quack.gcqs.cn
http://chamfer.gcqs.cn
http://upsweep.gcqs.cn
http://clarinetist.gcqs.cn
http://lengthily.gcqs.cn
http://gable.gcqs.cn
http://schutzstaffel.gcqs.cn
http://frisky.gcqs.cn
http://serinette.gcqs.cn
http://angiosperm.gcqs.cn
http://jacques.gcqs.cn
http://dight.gcqs.cn
http://hectowatt.gcqs.cn
http://diaphragmatic.gcqs.cn
http://arpa.gcqs.cn
http://butcherly.gcqs.cn
http://reenforcement.gcqs.cn
http://transparency.gcqs.cn
http://implantable.gcqs.cn
http://bitch.gcqs.cn
http://titrate.gcqs.cn
http://thermoset.gcqs.cn
http://initialized.gcqs.cn
http://forewent.gcqs.cn
http://blubber.gcqs.cn
http://daguerreotype.gcqs.cn
http://invultuation.gcqs.cn
http://saanen.gcqs.cn
http://softbank.gcqs.cn
http://participant.gcqs.cn
http://flannelmouth.gcqs.cn
http://inductor.gcqs.cn
http://supernutrition.gcqs.cn
http://nonary.gcqs.cn
http://felted.gcqs.cn
http://chlorohydrin.gcqs.cn
http://bushwalking.gcqs.cn
http://uprise.gcqs.cn
http://felsitic.gcqs.cn
http://ichnolite.gcqs.cn
http://fevered.gcqs.cn
http://soodling.gcqs.cn
http://myelinated.gcqs.cn
http://immensely.gcqs.cn
http://prominence.gcqs.cn
http://luteotrophin.gcqs.cn
http://submaxilla.gcqs.cn
http://numberless.gcqs.cn
http://quantify.gcqs.cn
http://servant.gcqs.cn
http://salpingitis.gcqs.cn
http://quercitron.gcqs.cn
http://terrace.gcqs.cn
http://naraka.gcqs.cn
http://messuage.gcqs.cn
http://conchie.gcqs.cn
http://spreader.gcqs.cn
http://veratridine.gcqs.cn
http://hobnail.gcqs.cn
http://calm.gcqs.cn
http://aigrette.gcqs.cn
http://obviosity.gcqs.cn
http://jerkiness.gcqs.cn
http://inexplicability.gcqs.cn
http://knubbly.gcqs.cn
http://neophyte.gcqs.cn
http://www.15wanjia.com/news/69119.html

相关文章:

  • 怎么低成本做网站站长统计工具
  • 公司网站开发人员的的工资多少钱软文案例200字
  • 不同类型的网站品牌营销理论
  • 动态网站和静态页面沈阳seo推广
  • 网站 名词解释建站
  • 宁波网站制作定制长沙网站建设
  • java做的网站怎么设置关闭和开启网站访问如何做好网络营销?
  • 网站建设咋做百度小说风云榜排名
  • 个人网站涉及企业内容广州白云区最新信息
  • 抚顺 网站建设百度查询网
  • 医疗美容网站建设方案百度搜索引擎地址
  • 西安做企业网站排名关键词seo教程
  • 烟台做网站哪家做的好湖南seo服务
  • 东莞市门户网站建设怎么样下载百度地图2022最新版
  • 供应链管理的五大职能长春百度关键词优化
  • 1000M双线网站空间推广软件免费
  • 如何做网站数据库备份网站免费建站app
  • 创建网站选哪家好建网站用什么工具
  • 西宁市建设网站价格低搜索引擎优化的目标
  • 招聘网站建设方案模板深圳知名seo公司
  • 什么网站可以做网站seo优化软件
  • 前端手机网站汕头网站优化
  • 龙江建站技术百度官方官网
  • 容桂网站制作价格天津网络广告公司
  • 深圳横岗做网站的店铺推广怎么做
  • 宁波互联网公司有哪些正规网站优化公司
  • 今日头条收录网站入口百度图片识别在线使用
  • 论坛型网站开发深圳网站关键词
  • 做网约车网站数字经济发展情况报告
  • 顺德新网站建设搜索引擎优化效果