記錄類別屬性狀態 (PropertyState)
透過在類別成員變數上宣告@PropertyState
註解,並使用PropertyStateSupport
工具類別配合,便可以輕鬆實現監控類別成員屬性的變更。
@PropertyState註解:宣告記錄類別成員屬性值的變更;
propertyName:成員屬性名稱,預設為空白則採用目前成員名稱;
aliasName:自訂別名,預設為空白;
setterName:成員屬性SET方法名稱,預設為空;
- ##範例程式碼:
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()); } } }
- 執行結果:
发生变更的字段名集合: [user_name, age] 已将user_name由123456变更为YMPer 已将年龄由20变更为30