001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.util; 018 019import java.beans.Introspector; 020 021import javax.servlet.ServletContextEvent; 022import javax.servlet.ServletContextListener; 023 024import org.springframework.beans.CachedIntrospectionResults; 025 026/** 027 * Listener that flushes the JDK's {@link java.beans.Introspector JavaBeans Introspector} 028 * cache on web app shutdown. Register this listener in your {@code web.xml} to 029 * guarantee proper release of the web application class loader and its loaded classes. 030 * 031 * <p><b>If the JavaBeans Introspector has been used to analyze application classes, 032 * the system-level Introspector cache will hold a hard reference to those classes. 033 * Consequently, those classes and the web application class loader will not be 034 * garbage-collected on web app shutdown!</b> This listener performs proper cleanup, 035 * to allow for garbage collection to take effect. 036 * 037 * <p>Unfortunately, the only way to clean up the Introspector is to flush 038 * the entire cache, as there is no way to specifically determine the 039 * application's classes referenced there. This will remove cached 040 * introspection results for all other applications in the server too. 041 * 042 * <p>Note that this listener is <i>not</i> necessary when using Spring's beans 043 * infrastructure within the application, as Spring's own introspection results 044 * cache will immediately flush an analyzed class from the JavaBeans Introspector 045 * cache and only hold a cache within the application's own ClassLoader. 046 * 047 * <b>Although Spring itself does not create JDK Introspector leaks, note that this 048 * listener should nevertheless be used in scenarios where the Spring framework classes 049 * themselves reside in a 'common' ClassLoader (such as the system ClassLoader).</b> 050 * In such a scenario, this listener will properly clean up Spring's introspection cache. 051 * 052 * <p>Application classes hardly ever need to use the JavaBeans Introspector 053 * directly, so are normally not the cause of Introspector resource leaks. 054 * Rather, many libraries and frameworks do not clean up the Introspector: 055 * e.g. Struts and Quartz. 056 * 057 * <p>Note that a single such Introspector leak will cause the entire web 058 * app class loader to not get garbage collected! This has the consequence that 059 * you will see all the application's static class resources (like singletons) 060 * around after web app shutdown, which is not the fault of those classes! 061 * 062 * <p><b>This listener should be registered as the first one in {@code web.xml}, 063 * before any application listeners such as Spring's ContextLoaderListener.</b> 064 * This allows the listener to take full effect at the right time of the lifecycle. 065 * 066 * @author Juergen Hoeller 067 * @since 1.1 068 * @see java.beans.Introspector#flushCaches() 069 * @see org.springframework.beans.CachedIntrospectionResults#acceptClassLoader 070 * @see org.springframework.beans.CachedIntrospectionResults#clearClassLoader 071 */ 072public class IntrospectorCleanupListener implements ServletContextListener { 073 074 @Override 075 public void contextInitialized(ServletContextEvent event) { 076 CachedIntrospectionResults.acceptClassLoader(Thread.currentThread().getContextClassLoader()); 077 } 078 079 @Override 080 public void contextDestroyed(ServletContextEvent event) { 081 CachedIntrospectionResults.clearClassLoader(Thread.currentThread().getContextClassLoader()); 082 Introspector.flushCaches(); 083 } 084 085}