In-depth understanding of Spring Series Thirteen: IntrospectorCleanupListener resolved

 

Introspector role and influence

Before analyzing IntrospectorCleanupListener, first look Introspector. Introspector class is under the JDK java.beans package, which provides an understanding of the original class methods, properties, and events of the standard method for the target JavaBean. In layman's terms, that is, by Introspector can build a BeanInfo object, and this object contains the target BeanInfo class properties, methods, and descriptions of events, then you can use this BeanInfo objects to target objects related operations.

Let's look at a simple example will be very easy to understand. For simplicity, Student class is only a name attribute.

 

 

 
结果输出:Student{name='张三'}

Introspector.getBeanInfo method by looking at the source code will find, Introspector in time to build a BeanInfo object, and the object will be to build the original class BeanInfo cache to a Map, the source code is as follows.

 

Can be drawn on by the code, Introspector indirectly holds a strong reference to the BeanInfo. If you use a lot of operating Introspector class, Introspector will indirectly hold a strong reference of these BeanInfo. In the event of garbage collection, detects the presence of these references BeanInfo chains, these classes and the corresponding class loader will not be garbage collected, leading to a memory leak. Therefore, in order to solve this problem, use Introspector after the operation is complete, call flushCaches Introspector class method of clearing the cache.

 

By the above code you will find the time to clear the entire cache is cleared, because there is no good way to determine which application belongs to each cache, so clear when the cache is cleared for all applications.

IntrospectorCleanupListener resolve

Analysis of the role and impact Introspector above, and that IntrospectorCleanupListener Introspector what does it matter?
IntrospectorCleanupListener is spring-web jar classes, as the source.

 

IntrospectorCleanupListener realized ServletContextListener interfaces, that is, the web container initialization (accurate to say that before filters or servlets initialization) will be executed when contextInitialized method, destroyed ServletContext (accurate to say that after the destruction of filters and servlets) when performs contextDestroyed method. From the figure contextDestroyed method, you can be seen at the time of the destruction of the ServletContext called Introspector.flushCaches method corresponding to empty the cache. IntrospectorCleanupListener Why do that? Is Spring does not correspond to empty the cache after use Introspector operation? View IntrospectorCleanupListener class source code, you will find there is such a mark.

 

The effect that the listener does not need to use this when using Spring itself, Spring because of its own internal mechanism will immediately clear the corresponding cache. Although, Spring itself, so there is no problem, but if used in combination with other frameworks, while others have this problem frameworks, such as Struts, Quartz, etc., then you need to configure the listener, empty the cache at the corresponding time of the destruction of the ServletContext.

One thing to note is that, like a simple Introspector memory leak will cause the entire application class loader will not be garbage collected, if there is a memory leak problem, consider this factor.

Configuration IntrospectorCleanupListener

In previous work experience, we see web.xml repeatedly arranged in the non-first IntrospectorCleanupListener listener.

 

In fact, I read the source knows that the official statement is this listener must be configured in web.xml first listener, can play the most effective role at the right time.

The reason is simple, before Servlet3.0 specification, calling the listener is random, but from the beginning Servlet3.0, listeners calling sequence is the order of its configuration in web.xml according to the listener and to achieve ServletContextListener , the contextInitialized positive sequence order of method calls are sequentially performed in the order arranged in web.xml, the call sequence contextDestroyed process is performed in the order reverse arranged successively in web.xml. So, if IntrospectorCleanupListener configured it became the first listener, then it will last a contextDestroyed method execution, will play the most effective scavenging effect; and if not, it may remain in the cache is not cleared.

Guess you like

Origin www.cnblogs.com/liuys635/p/12594313.html