IntrospectorCleanupListener 简介

本文介绍org.springframework.web.util.IntrospectorCleanupListener的作用及其用法。该监听器用于解决JavaBeanIntrospector缓存泄露问题,确保web应用关闭时能够正确释放与应用相关的类加载器及管理的类。通过在web.xml中配置该监听器,可以避免因缓存泄露导致的类加载器无法被垃圾回收的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

import java.beans.Introspector;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class IntrospectorCleanupListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
}

public void contextDestroyed(ServletContextEvent event) {
Introspector.flushCaches();
}
}

用法如下

用法很简单,就是在web.xml中加入:
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值