`

spring有三种启动方式

阅读更多

     纵观java的开源框架,strtus,spring或者是springmvc等,他们的启动都有个共同的特点就是在服务器启动的时候预先加载进来相应的信息,初始化相关的类,他们或者用Servlet实现如sturts1和springmvc,或者用监听器和过滤器,总之一切的准备工作都是对容器里所有的类进行加载和调用的管理以及更好的解耦合。下面主要讨论spring启动的三种常见方式。

       spring启动可以选中监听器也可以选择过滤器一下要介绍的三种方式是:IntrospectorCleanupListener初始化spring容器,ContextLoadListener启动spring及ContextLoaderServlet

IntrospectorCleanupListener简介

spring中的提供了一个名为org.springframework.web.util.IntrospectorCleanupListener的监听器。它主要负责处理由 JavaBeans Introspector的使用而引起的缓冲泄露。spring中对它的描述如下:
 
它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.在web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类
 
如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web 应用相关的class loader没有被垃圾回收.
 
不幸的是,清除Introspector的唯一方式是刷新整个缓冲.这是因为我们没法判断哪些是属于你的应用的引用.所以删除被缓冲的introspection会导致把这台电脑上的所有应用的introspection都删掉.
 
需要注意的是,spring 托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javaBeans Introspector缓冲中清除掉.
 
应用程序中的类从来不直接使用JavaBeans Introspector.所以他们一般不会导致内部查看资源泄露.但是一些类库和框架往往会产生这个问题.例如:Struts 和Quartz.
 
单个的内部查看泄漏会导致整个的web应用的类加载器不能进行垃圾回收.在web应用关闭之后,你会看到此应用的所有静态类资源(例如单例).这个错误当然不是由这个类自身引起的.

 

ContextLoaderListener简介

与BeanFactory通常以编程的方式被创建不同的是,ApplicationContext能以声明的方式创建,如使用ContextLoader。当然你也可以使用ApplicationContext的实现之一来以编程的方式创建ApplicationContext实例。首先,让我们先分析ContextLoader接口及其实现。

    ContextLoader接口有两个实现:ContextLoaderListener和ContextLoaderServlet。两者都实现同样的功能,但不同的是,ContextLoaderListener不能在与Servlet 2.2兼容的web容器中使用。根据Servlet 2.4规范, servlet context listener要在web应用程序的servlet context建立后立即执行,并要能够响应第一个请求(在servlet context要关闭时也一样):这样一个servlet context listener是初始化Spring ApplicationContext的理想场所。虽然使用哪个完全取决于你,但是在同等条件下应该首选ContextLoaderListener;对于更多兼容性的信息,请查看ContextLoaderServlet的JavaDoc。

    你可以象下面那样使用ContextLoaderListener来注册一个ApplicationContext:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- or use the ContextLoaderServlet instead of the above listener
<servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
-->

    监听器首先检查contextConfigLocation参数,如果它不存在,它将使用/WEB-INF/applicationContext.xml作为默认值。如果已存在,它将使用分隔符(逗号、冒号或空格)将字符串分解成应用上下文将位置路径。ContextLoaderServlet同ContextLoaderListener一样使用'contextConfigLocation'参数。

ContextLoaderServlet简介

     先看看spring官方网上的信息:

Bootstrap servlet to start up Spring's root WebApplicationContext. Simply delegates to ContextLoader.

This servlet should have a lower load-on-startup value in web.xml than any servlets that access the root web application context.

Note that this class has been deprecated for containers implementing Servlet API 2.4 or higher, in favor of ContextLoaderListener.
According to Servlet 2.4, listeners must be initialized before load-on-startup servlets. Many Servlet 2.3 containers already enforce this behavior. If you use such a container, this servlet can be replaced with ContextLoaderListener. Else or if working with a Servlet 2.2 container, stick with this servlet.

(大意是说,这个servlet是启动spring容器的一个servlet,必须把启动优先级调到0,就是比所有的servlet都先启动,ContextServlet最好用在servlet2.4或以上虽然2.3有支持)

0
0
分享到:
评论

相关推荐

    spring boot三种启动方式.txt

    spring boot三种启动方式.txt

    spring配置和启动方式

    spring配置和启动方式 博客地址:https://blog.csdn.net/u010476739/article/details/76696756

    spring famework 配置启动方式

    本篇文章将深入探讨Spring Framework的两种主要配置启动方式:XML配置和注解配置,并通过具体的示例来阐述这两种方法。 首先,让我们从XML配置开始。在Spring早期版本中,XML配置是最常见的方式,它通过定义Bean的...

    spring boot如何指定启动端口

    Spring Boot 提供了两种方式来实现动态指定端口: 方式三-1:命令行方式 可以在命令行中使用以下命令来指定启动端口: `java -jar test.jar --server.port=8081` 这样,在启动 Spring Boot 应用程序时,端口号将...

    spring项目启动加载类到静态服务类中的三种方式.zip

    本教程将详细阐述三种不同的方式,将Spring项目启动时加载的类集成到静态服务类中。 1. **使用`ApplicationContextAware`接口** `ApplicationContextAware`是Spring提供的一个接口,它允许我们在类中注入...

    Spring启动完整流程图

    Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动完整流程图 Spring启动...

    SpringCloud服务启动脚本

    SpringCloud微服务架构,启动脚本,动态输出日志,并指向启动日志脚本位置。

    spring学习:依赖注入的几种方式讨论

    Spring Boot引入了一种更智能的依赖注入方式——自动配置。自动配置是通过`@EnableAutoConfiguration`注解启动的,它根据项目中的类路径和特定条件自动配置Bean。例如,如果类路径下存在`MongoClient`的jar,Spring ...

    spring boot windows 启动脚本

    spring boot windows 启动脚本

    Spring 自启动项目demo

    当Spring容器加载时,它可以自动扫描指定包下的所有类,发现带有特定注解(如@Service、@Repository、@Component等)的类,并将它们作为Bean进行实例化。这些Bean在应用程序启动时会自动创建,无需手动调用new关键字...

    spring boot/spring cloud项目启动脚本,默认名称:start.sh

    linux服务器,springboot,spring cloud、spring cloud alibaba等项目启动脚本 下载脚本, 1,上传脚本至jar包同级目录 2,更改脚本: jar包名称 项目文件路径 日志路径(包含日志名称) 脚本已配置好jvm优化...

    spring获取weblogic jndi数据源的两种方式

    ### Spring 获取 WebLogic JNDI 数据源的两种方式 在Spring框架中,通过JNDI(Java Naming and Directory Interface)可以方便地访问WebLogic服务器中的数据源。这为应用程序提供了高度解耦的数据访问机制,使得...

    Java+Spring+Spring容器启动流程方法调用xmind脑图

    1. Spring容器的启动流程 2. 循环依赖 3. Spring 中Bean的创建 4. Spring 方法xmind脑图

    spring容器启动和关闭时事件监听

    spring容器启动和关闭时事件监听;spring容器启动和关闭时事件监听;spring容器启动和关闭时事件监听

    Spring-Boot启动Demo

    **Spring Boot启动方式详解** ...理解这两种启动方式有助于你根据项目需求做出合适的选择。在实践中,你可以根据描述中的`demo-war`进一步学习和实践Spring Boot的war打包和部署过程,从而加深对这一启动方式的理解。

    spring的三种任务调度实现方式lib

    Spring提供了三种主要的任务调度实现方式:Spring内置的任务调度器(TaskScheduler),Quartz Scheduler,以及Spring Batch。下面我们将详细介绍这三种方式。 1. Spring内置的任务调度器(TaskScheduler) Spring...

    spring boot部署启动脚本

    标题中的“Spring Boot部署启动脚本”指的是在Spring Boot应用开发完成后,为了自动化部署和启动应用程序而创建的脚本。Spring Boot是一个简化Spring应用程序开发的框架,它提倡“开箱即用”的理念,使得开发者可以...

    spring和quartz的定时器的启动和停止例子

    Spring提供了一种集成Quartz的方式,使得我们可以方便地在Spring应用中管理和执行定时任务。本篇将深入探讨如何在Spring中启动和停止Quartz定时器。 首先,我们需要理解Spring和Quartz的基本概念。Spring是一个强大...

    82. Spring Boot – 启动彩蛋【从零开始学Spring Boot】

    Spring Boot – 启动彩蛋"这一主题,这属于Spring Boot框架的一部分,该框架简化了Java应用程序的创建和管理。启动彩蛋是开发人员为了增加趣味性或者隐藏信息而在软件中设置的小秘密,通常需要特定的触发条件才能...

Global site tag (gtag.js) - Google Analytics