开发这么久,我也没有使用过IntrospectorCleanupListener监听器,今天偶尔看到一篇文章,虽然没有怎么读懂,也不太理解,但是好像给官方提供一些解释!给自己留一个备注,多点东西因为没有什么问题吧!
我想应该对整个程序没有太大的影响,在部署描述符web.xml中添加如下的监听器
org.springframework.web.util.IntrospectorCleanupListener
可能新的版本中没有这个问题吧,也许吧,自己没有遇到这样的问题!
spring的说明如下:
Listener that flushes the JDK's [JavaBeans Introspector
](http://java.sun.com/javase/6/docs/api/java/beans/Introspector.html?is-external=true) cache on web app shutdown. Register this listener in your web.xml
to guarantee proper release of the web application class loader and its loaded classes.
**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!** This listener performs proper cleanup, to allow for garbage collection to take effect.
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.
Note that this listener is *not* 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. **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).** In such a scenario, this listener will properly clean up Spring's introspection cache.
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.
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!
**This listener should be registered as the first one in web.xml
, before any application listeners such as Spring's ContextLoaderListener.** This allows the listener to take full effect at the right time of the lifecycle.
翻译如下
其中JavaBeans Introspector是一个类,位置在Java.bean.Introspector,这个类的用途是发现java类是否符合javaBean规范,也就是这个类是不是javabean。具体用法可以参照jdk文档;
上面的意思就是,如果有的框架或者程序用到了JavaBeans Introspector了,那么就启用了一个系统级别的缓存,这个缓存会存放一些曾加载并分析过的javabean的引用,当web服务器关闭的时候,由于这个缓存中存放着这些javabean的引用,所以垃圾回收器不能对web容器中的javaBean对象进行回收,导致内存越来越大。
spring提供的org.springframework.web.util.IntrospectorCleanupListener就解决了这个问题,他会在web服务器停止的时候,清理一下这个Introspector缓存。使那些javabean能被垃圾回收器正确回收。
spring不会出现这种问题,因为spring在加载并分析完一个类之后会马上刷新JavaBeans Introspector缓存,这样就保证了spring不会出现这种内存泄漏的问题。
但是有很多程序和框架在使用了JavaBeans Introspector之后,都没有进行清理工作,比如quartz、struts;解决办法很简单,就是上面的那个配置。