The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
只知道java反射,宁知道内省吗?

1. preface

In the previous article, we encapsulated aMybatisUniversal Mapper. I use reflection to get entity class attributes. Most students also first felt that they could use reflection to achieve this. In fact, there is another technology that can also achieve it, which is introspection (Introspector)。

2. What is introspection

In computer science, introspection refers to the process of a computer program when it is running (Runtime) Inspection object (Object), which can also be commonly referred to as runtime type checking. Introspection should not be confused with reflection. Reflecting goes a step further than introspection, when a computer program is running (Runtime) An ability to access, detect, and modify its own state or behavior.

JavaIntrospection in the book is a right wayJavaBeanA default handling method for attribute,. Is it a little confusing after reading the concept? I am the same. So let's write an example to see. You need to understand before writingJavaBeanDefinition of;

  • Attributes are private.

  • with or without referencepublicConstruction method.

  • There are public statements for these attributesgetter/settermethod.

But if some classes are more "naughty", there aregetter/setterWithout a corresponding entity, it is easy to be introspective, but exceptions will be thrown. So abiding by the rules is a good way for us to use introspection.

in fact add/removeis Also considered an operationJavaBeanAttribute methods, so we have to be careful.

3. Java introspective operations

JavaBeanGenerally used to transfer data, our database entity class is a typicalJavaBean

public class UserInfo implements Serializable {

    private static final long serialVersionUID = -8938650956516110149L;
    private Long userId;
    private String name;
    private Integer age;
    private String time;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    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;
    }
}

Please note that I specifically did not givetimeproperty is setgetter/setter。Next, I will demonstrate the use of introspection to manipulate entities.

Javabyjava.beans.Introspectorto perform introspective operations. Commonly used introspection operations include the following,Of course, there are other additional types

Java 内省

3.1 BeanInfo

BeanInfoIs an overall description of JavaBean by introspection. Here I just want to getUserInfo, so it should be written like this:

BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class,Object.class);

you can stopflag Two attributes control the depth of introspective analysis.

3.2 BeanDescriptor

and we'll seeBeanDescriptorWhat are the main ones:

BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
// cn.felord.kono.entity.UserInfo.class
Class<?& gt; beanClass = beanDescriptor.getBeanClass();
// UserInfo
String name = beanDescriptor.getName();

turned out to beJavaBeantheClassType and name.

3.3 PropertyDescriptors

PropertyDescriptoris to describeJavaBean, let's print it to have a look

Stream.of(beanInfo.getPropertyDescriptors())
        .forEach(System.out::println);
java.beans.PropertyDescriptor[name=age; propertyType=class java.lang.Integer; readMethod=public java.lang.Integer cn.felord.kono.entity.UserInfo.getAge(); writeMethod=public void cn.felord.kono.entity.UserInfo.setAge(java.lang.Integer)]
java.beans.PropertyDescriptor[name=name; propertyType=class java.lang.String; readMethod=public java.lang.String cn.felord.kono.entity.UserInfo.getName(); writeMethod=public void cn.felord.kono.entity.UserInfo.setName(java.lang.String)]
java.beans.PropertyDescriptor[name=userId; propertyType=class java.lang.Long; readMethod=public java.lang.Long cn.felord.kono.entity.UserInfo.getUserId(); writeMethod=public void cn.felord.kono.entity.UserInfo.setUserId(java.lang.Long)]

originalPropertyDescriptorObject that contains member attributesnametypeMethod of readingHow to write。No. It doesn't includetimeAttribute because it does not havegetter/setterignored.

3.4 MethodDescriptors

As the name suggests, it must be a descriptionJavaBeanof the methods. A printout is displayed here.

java.beans.MethodDescriptor[name=foo; method=public static void cn.felord.kono.entity.UserInfo.foo(java.lang.String)]

Contains the method name and method object (Method)。Notice the strange way to blend in here foo, yes! I wrote a random method. MethodDescriptors can containJavaBeanUnder all methods, including static methods. Of course, it is limited by the depth of introspection.

3.5 EventSetDescriptors

Currently, the print is empty. I don't know what the functions of some paradigms related to publishing and subscribing JavaBean events are currently.

4. summary

JavaReflection obtains all information about a class at runtime and can manipulate the fields, methods, constructors, etc. of the class. It is very powerful. Introspection is actually a subset of reflection, based on reflection implementation. specialized operationsJavaBeanYes, only focus onJavaBeanSome properties of the properties, methods, and events of. Now that I understand this, I think it is necessary to put universalMapperRefactoring it.

read more
Resource download
PriceFree
Customer Service QQ: 138338438
Original link:https://bcbccb.cn/en/4636.html, please indicate the source for reprinting.
1

Comments0

【niushop开源商城旗舰版v3.7】商城源码+分销版+破解版+企业版+虚拟商品+第三方对接
[niushop Open Source Mall Ultimate Version v3.7] Mall source code + distribution version + cracked version + enterprise version + virtual goods + third-party docking
Someone bought it 10 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish