Detailed explanation of @Accessors annotation


foreword

In your work, you may sometimes see annotations like @Accessors(chain = true), which is an annotation in the lombok plugin package, so what does it mean?

1. @Accessors source code

We open the source code of @Accessors and see:

(1) The main function of this annotation is to make some related settings when the property field generates the getter and setter methods.

(2) When it can act on a class, it modifies all fields in the class, and when it is applied to a specific field, it is only valid for this field.

There are three attributes in this field, namely fluent, chain, and prefix. Let's explain them separately. What do they mean?

2. @Accessors attribute description

2.1 fluent attribute

If not written, the default is false. When the value is true, there is no get in front of the getter method of the corresponding field, and there is no set in the setter method.

2.2 chain attribute

If not written, the default is false. When the value is true, the current object will be returned after the setter method of the corresponding field is called.

2.3 prefix attribute

The property is a string array. When the array has a value, it means that the corresponding prefix in the field is ignored and the corresponding getter and setter methods are generated.

For example, there are now xxName field and yyAge field, xx and yy are the prefixes of name field and age field respectively.

Then, the getter and setter methods we are generating are as follows, which are also prefixed with xx and yy.

If we add its prefix to the property value of @Accessors, we can call the field's getter and setter methods as if there was no prefix.

 

 

 

Guess you like

Origin blog.csdn.net/sunnyzyq/article/details/119992746