过滤器总结一(IntrospectorCleanupListener)

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应用关闭之后,你会看到此应用的所在静态资源如单例,这个错误不是由该类自身引起的.

 

java内省(Introspector)简介

 

内省是 Java 语言对 Bean 类属性、事件的一种缺省处理方法。例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这就是默认的规则。 Java 中提供了一套 API 用来访问某个属性的 getter/setter 方法,通过这些 API 可以使你不需要了解这个规则(但你最好还是要搞清楚),这些 API 存放于包 java.beans 中。                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
一般的做法是通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法。下面我们来看一个例子,这个例子把某个对象的所有属性名称和值都打印出来:                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
import java.beans.BeanInfo;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import java.beans.Introspector;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import java.beans.PropertyDescriptor;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  * 内省演示例子                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           *  */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
public class IntrospectorDemo {                                                                                                                                          

  public static void main(String[] args) throws Exception{                                                                                                                  

 IntrospectorDemo demo = new IntrospectorDemo();                                                                                                                       

  demo.setName( "Winter Lau" );                                                      

 // 如果不想把父类的属性也列出来的话,                                              

// 那 getBeanInfo 的第二个参数填写父类的信息                                       

BeanInfo bi = Introspector.getBeanInfo(demo.getClass(), Object. class );                                                                                                 

PropertyDescriptor[] props = bi.getPropertyDescriptors();  
        for ( int i=0;i<props.length;i++){   

  System.out.println(props[i].getName()+ "=" +prop[i].getReadMethod().invoke(demo,null));                                                                                                                                                                                            
        }                                                                           

    }                                                                              

 

 

  public String getName() {                        

                                     

      return name;                                    

                            

    }       

     public void setName(String name) {                                                                                                         
      this .name=name;   

}                                                                                                                                             
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
Web 开发框架 Struts 中的 FormBean 就是通过内省机制来将表单中的数据映射到类的属性上,因此要求 FormBean 的每个属性要有 getter/setter 方法。但也并不总是这样,什么意思呢?就是说对一个 Bean 类来讲,我可以没有属性,但是只要有 getter/setter 方法中的其中一个,那么 Java 的内省机制就会认为存在一个属性,比如类中有方法 setMobile ,那么就认为存在一个 mobile 的属性,这样可以方便我们把 Bean 类通过一个接口来定义而不用去关心具体实现,不用去关心 Bean 中数据的存储。比如我们可以把所有的 getter/setter 方法放到接口里定义,但是真正数据的存取则是在具体类中去实现,这样可提高系统的扩展性。

 

你可能感兴趣的:(java,spring,bean,Web,quartz)