Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the related classes of Java introspection Introspector

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the related classes of Java introspection Introspector". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn what are the related classes of Java introspection Introspector!

What is JavaBean?

JavaBean is a special (in fact, ordinary, not very special) class, mainly used to pass data information, the methods in this class are mainly used to access private fields, and the method names conform to some naming convention (the fields are private, each field has Setter and Getter methods, and the method and field naming meets the first letter lowercase hump naming convention). If you pass information between two modules, you can encapsulate the information in a JavaBean, which is called a value object (Value Object) or VO. This information is stored in private variables of the class and is obtained through the Setter and Getter methods. The corresponding concept of JavaBean information in Introspector is BeanInfo, which contains all the Descriptor (descriptors) of JavaBean, mainly PropertyDescriptor,MethodDescriptor (MethodDescriptor contains ParameterDescriptor), BeanDescriptor and EventSetDescriptor.

The difference between property Field and property description PropertiesDescriptor

If it is a strict JavaBean (the Field name is not duplicated, and Field has Setter and Getter methods), its PropertyDescriptor will parse the Setter and Getter methods, merge the parsing results, and finally get the corresponding PropertyDescriptor instance. So PropertyDescriptor contains the property name and the property's Setter and Getter methods, if any.

Introspection of the difference between Introspector and reflection Reflection Reflection: reflection is the runtime to obtain all the information of a class, you can get all the defined information of the class (including member variables, member methods, constructors, etc.) can manipulate the fields, methods, constructors and other parts of the class. Can be thought of as specular reflection or looking in the mirror, this operation is objective, that is, the class information obtained by reflection must be correct. Introspector: introspection is implemented based on reflection, which is mainly used to operate JavaBean. Bean information descriptors are parsed based on JavaBean specifications. Class descriptors can be obtained according to the Setter and Getter methods of the class. You can think of it as "self-reflection", which is subjective and not necessarily correct (if the properties in a class do not have Setter and Getter methods, you cannot use Introspector). Commonly used Introspector related classes

This paper mainly introduces the methods provided by several core classes.

Introspector

Introspector is similar to BeanInfo's static factory class, mainly providing static methods to get the BeanInfo through the Class instance, and after getting the BeanInfo, you can get other descriptors. Main methods:

Public static BeanInfo getBeanInfo (Class beanClass): get the BeanInfo instance through the Class instance. BeanInfo

BeanInfo is an interface, specifically implemented as GenericBeanInfo, through which you can get various types of descriptors for a class. Main methods:

BeanDescriptor getBeanDescriptor (): gets the JavaBean descriptor. EventSetDescriptor [] getEventSetDescriptors (): get all the EventSetDescriptor of JavaBean. PropertyDescriptor [] getPropertyDescriptors (): get all the PropertyDescriptor of JavaBean. MethodDescriptor [] getMethodDescriptors (): get all the MethodDescriptor of JavaBean.

It should be noted here that in the PropertyDescriptor array obtained through BeanInfo#getPropertyDescriptors (), in addition to the Bean attribute, "there will be a PropertyDescriptor instance named class", which comes from the getClass method of Class. If you do not need this attribute, it is best to judge and filter, which needs to be kept in mind, otherwise it is prone to problems.

PropertyDescriptor

The PropertyDescriptor class means that the JavaBean class exports an attribute through memory (Setter and Getter), which should be the most common class in the introspection system. Main methods:

Synchronized Class getPropertyType (): the Class object that gets the property. Synchronized Method getReadMethod (): gets the method for reading the attribute value (Getter); synchronized Method getWriteMethod (): gets the method for writing the attribute value (Setter). Int hashCode (): gets the hash value of the object. Synchronized void setReadMethod (Method readMethod): sets the method used to read the attribute value (Getter). Synchronized void setWriteMethod (Method writeMethod): sets the method used to write the property value (Setter).

For example:

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 result:

Age

SetAge

GetAge

=

Id

SetId

GetId

=

Name

SetName

GetName

Improper use of Introspector can cause memory overflow

If the framework or program uses JavaBeans Introspector, it is equivalent to "enabling a system-level cache", which stores references to Javabean that have been loaded and analyzed. When the Web server is shut down, because these Javabean references are stored in the cache, the garbage collector cannot collect 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 flush the entire cache buffer, because JDK cannot determine which references belong to the current application, so flushing the entire Introspector cache buffer will result in the deletion of all applied Introspector caches on the server. The org.springframework.web.util.IntrospectorCleanupListener provided in Spring is designed to solve this problem by cleaning up the Introspector cache when the Web server stops so that those Javabean can be properly reclaimed by the garbage collector.

In other words, JDK's Introspector cache management is flawed. However, this problem will not occur if it is used in the Spring system, because Spring transfers the management of the Introspector cache to Spring itself rather than JDK (or completely after the Web container is destroyed). After loading and parsing all the classes, the Introspector cache is cleaned against the class loader to avoid memory leaks. For more information, please see the method AbstractApplicationContext#resetCommonCaches () that cleans up the cache in the finally code block of CachedIntrospectionResults and SpringBoot refresh context method AbstractApplicationContext#refresh () . However, many programs and frameworks do not clean up after using JavaBeans Introspector, such as Quartz, Struts and so on. This kind of operation will become a hidden danger of memory leakage.

At this point, I believe you have a deeper understanding of "Java introspection Introspector related classes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report