Record class property state (PropertyState)


By declaring the @PropertyState annotation on the class member variables and using the PropertyStateSupport tool class, you can easily monitor changes in class member attributes.

  • @PropertyState annotation: declares to record changes in class member property values;

    propertyName: member property name, the default is empty and the current member name is used;

    aliasName: Custom alias, the default is empty;

    setterName: Member attribute SET method name, the default is empty;

  • Sample code :

    public class PropertyStateTest {
    
        @PropertyState(propertyName = "user_name")
        private String username;
    
        @PropertyState(aliasName = "年龄")
        private int age;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public static void main(String[] args) throws Exception {
            PropertyStateTest _original = new PropertyStateTest();
            _original.setUsername("123456");
            _original.setAge(20);
            //
            PropertyStateSupport<PropertyStateTest> _support = PropertyStateSupport.create(_original);
            PropertyStateTest _new = _support.bind();
            _new.setUsername("YMPer");
            _new.setAge(30);
            //
            System.out.println("发生变更的字段名集合: " + Arrays.asList(_support.getChangedPropertyNames()));
            for (PropertyStateSupport.PropertyStateMeta _meta : _support.getChangedProperties()) {
                System.out.println("已将" + StringUtils.defaultIfBlank(_meta.getAliasName(), _meta.getPropertyName()) + "由" + _meta.getOriginalValue() + "变更为" + _meta.getNewValue());
            }
        }
    }
  • Execution result:

    发生变更的字段名集合: [user_name, age]
    已将user_name由123456变更为YMPer
    已将年龄由20变更为30