Talk about Java introspection introspector

premise

This article mainly analyzes the introspector Introspector is a tool class that deals specifically with JavaBeans and is used to obtain the description symbols in JavaBeans. The commonly used description symbols of JavaBeans and related classes include beaninfo, propertydescriptor, methoddescriptor, beandescriptor, eventsetdescriptor and parameterdescription tor。 The following will slowly analyze the usage of these classes and some characteristics of introspector.

What is a JavaBean

JavaBean is a special class (in fact, it is normal, not very special). It is mainly used to transfer data information. The methods in this class are mainly used to access private fields, and the method name conforms to some naming rules (fields are private, and each field has setter and getter methods, and the naming of methods and fields meets the naming rules of initial lowercase hump). If information is passed between two modules, the information can be encapsulated in JavaBean, which is called value object (value object) or vo. These information are stored in private variables of the class and obtained through setter and getter methods. The corresponding concept of JavaBean information in the introspector is beaninfo, which contains all descriptors (descriptors) of JavaBeans, mainly propertydescriptors, Methoddescriptor (the methoddescriptor contains parameterdescriptor), beandescriptor and eventsetdescriptor.

Difference between property field and propertiesdescriptor

If it is a strict JavaBean (the field name is not repeated, and the field has setter and getter methods), its propertydescriptor will combine the parsing results by parsing setter and getter methods, and finally get the corresponding propertydescriptor instance. Therefore, the propertydescriptor contains the property name and the setter and getter methods of the property (if any).

The difference between introspector and reflection

Common introspector related classes

It mainly introduces the methods provided by several core classes.

Introspector

Introspector is similar to the static factory class of beaninfo. It mainly provides static methods to obtain beaninfo through class instances. After obtaining beaninfo, you can obtain other descriptors. Main methods:

BeanInfo

Beaninfo is an interface. The specific implementation is genericbeaninfo. Through this interface, you can obtain various types of descriptors of a class. Main methods:

It should be noted here that the propertydescriptor array obtained through beaninfo#getpropertydescriptors() also contains a propertydescriptor instance with the property name class in addition to the bean property. Its source is the getClass method of class. If this property is not required, it is best to filter after judgment. This should be kept in mind, otherwise it is prone to problems.

PropertyDescriptor

The propertydescriptor class indicates that JavaBean class exports a property through memory (setter and getter). It should be the most common class in introspection system. Main methods:

for instance:

public class Main {

    public static void main(String[] args) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            if (!"class".equals(propertyDescriptor.getName())) {
                System.out.println(propertyDescriptor.getName());
                System.out.println(propertyDescriptor.getWriteMethod().getName());
                System.out.println(propertyDescriptor.getReadMethod().getName());
                System.out.println("=======================");
            }
        }
    }

    public static class Person {

        private Long id;
        private String name;
        private Integer age;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

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

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }
}

Output results:

age
setAge
getAge
=======================
id
setId
getId
=======================
name
setName
getName
=======================

Improper use of the introspector can lead to memory overflow

If the framework or program uses JavaBeans introspector, it is equivalent to enabling a system level cache. This cache will store the references of some JavaBeans that have been loaded and analyzed. When the web server is shut down, because the references of these JavaBeans are stored in this cache, the garbage collector cannot recycle the JavaBean objects in the web container, Resulting in more and more memory. It is also worth noting that the only way to clear the introspector cache is to refresh the entire cache buffer. This is because JDK cannot judge which references belong to the current application. Therefore, refreshing the entire introspector cache buffer will delete the introspector cache of all applications on the server. Org.com available in spring springframework. web. util. The introspector cleanuplistener is designed to solve this problem. It will clean up the introspector cache when the web server stops, so that those Java beans can be properly recycled by the garbage collector.

In other words, the introspector cache management of JDK has some defects. However, if it is used in the spring system, this problem will not occur, Because spring transfers the management of introspector cache to spring itself rather than JDK (or completely ignore it after the web container is destroyed). After loading and analyzing all classes, the introspector cache will be cleaned for the class loader to avoid memory leakage. For details, see cachedintrospectionresults and springboot refresh context method abstractapplicationcontext#refresh() There is a method abstractapplicationcontext#resetcommoncaches(); to clean up the cache in the finally code block in;. However, many programs and frameworks do not clean up after using JavaBeans introspector, such as quartz and struts. Such operations will become a hidden danger of memory leakage.

Summary

(end of this paper e-a-20200811 c-1-d)

This is the original article released by the official account Throwable, which is featured in the album "Java foundation and advanced."

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>