org.springframework.web.util.IntrospectorCleanupListener的用途

时间:2021-11-20 22:51:42

Spring官方API中对其描述如下

/**
* Listener that flushes the JDK's {@link java.beans.Introspector JavaBeans Introspector}
* cache on web app shutdown. Register this listener in your <code>web.xml</code> to
* guarantee proper release of the web application class loader and its loaded classes.
*
* <p><b>If the JavaBeans Introspector has been used to analyze application classes,
* the system-level Introspector cache will hold a hard reference to those classes.
* Consequently, those classes and the web application class loader will not be
* garbage-collected on web app shutdown!</b> This listener performs proper cleanup,
* to allow for garbage collection to take effect.
*
* <p>Unfortunately, the only way to clean up the Introspector is to flush
* the entire cache, as there is no way to specifically determine the
* application's classes referenced there. This will remove cached
* introspection results for all other applications in the server too.
*
* <p>Note that this listener is <i>not</i> necessary when using Spring's beans
* infrastructure within the application, as Spring's own introspection results
* cache will immediately flush an analyzed class from the JavaBeans Introspector
* cache and only hold a cache within the application's own ClassLoader.
*
* <b>Although Spring itself does not create JDK Introspector leaks, note that this
* listener should nevertheless be used in scenarios where the Spring framework classes
* themselves reside in a 'common' ClassLoader (such as the system ClassLoader).</b>
* In such a scenario, this listener will properly clean up Spring's introspection cache.
*
* <p>Application classes hardly ever need to use the JavaBeans Introspector
* directly, so are normally not the cause of Introspector resource leaks.
* Rather, many libraries and frameworks do not clean up the Introspector:
* e.g. Struts and Quartz.
*
* <p>Note that a single such Introspector leak will cause the entire web
* app class loader to not get garbage collected! This has the consequence that
* you will see all the application's static class resources (like singletons)
* around after web app shutdown, which is not the fault of those classes!
*
* <p><b>This listener should be registered as the first one in <code>web.xml</code>,
* before any application listeners such as Spring's ContextLoaderListener.</b>
* This allows the listener to take full effect at the right time of the lifecycle.
*
* @author Juergen Hoeller
* @since 1.1
* @see java.beans.Introspector#flushCaches()
* @see org.springframework.beans.CachedIntrospectionResults#acceptClassLoader
* @see org.springframework.beans.CachedIntrospectionResults#clearClassLoader
*/

翻译过来大致就是下面的意思

/**
* org.springframework.web.util.IntrospectorCleanupListener的用途
* @see -------------------------------------------------------------------------------------------------------------------
* @see 此监听器出用于主要为了解决java.beans.Introspector导致内存泄漏的问题
* @see 此监听器应该配置在web.xml中与Spring相关监听器中的第一个位置(也要在ContextLoaderListener的前面)
* @see -------------------------------------------------------------------------------------------------------------------
* @see JDK中的java.beans.Introspector类的用途是发现Java类是否符合JavaBean规范
* @see 如果有的框架或程序用到了Introspector类,那么就会启用一个系统级别的缓存,此缓存会存放一些曾加载并分析过的JavaBean的引用
* @see 当Web服务器关闭时,由于此缓存中存放着这些JavaBean的引用,所以垃圾回收器无法回收Web容器中的JavaBean对象,最后导致内存变大
* @see 而org.springframework.web.util.IntrospectorCleanupListener就是专门用来处理Introspector内存泄漏问题的辅助类
* @see IntrospectorCleanupListener会在Web服务器停止时清理Introspector缓存,使那些Javabean能被垃圾回收器正确回收
* @see -------------------------------------------------------------------------------------------------------------------
* @see Spring自身不会出现这种问题
* @see 因为Spring在加载并分析完一个类之后会马上刷新JavaBeans Introspector缓存,这就保证Spring中不会出现这种内存泄漏的问题
* @see 但有些程序和框架在使用了JavaBeans Introspector之后,没有进行清理工作(如Quartz,Struts),最后导致内存泄漏
* @see -------------------------------------------------------------------------------------------------------------------
* @create Sep 24, 2013 9:10:09 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/