@Accessors
is an annotation in the Lombok library for custom generationgetterandsetterThe behavior of the method.
It usually has@Getter
and@Setter
Annotations are used together to simplify code and enhance readability.
The following is@Accessors
Detailed introduction:
1. Basic functions
@Accessors
Allows developers to control the naming rules and behavior of generated getter and setter methods through configuration parameters. It supports the following parameters:
parameter | effect |
---|---|
chain | Whether to enable chained calls (Fluent API), default to false. |
fluent | Whether to generate a concise getter/setter method name (without get/set prefix), default to false. |
prefix | Specify field name prefixes, which are ignored by the generated getter/setter method. |
2. Use examples
2.1 Default Behavior
import ; import ; import ; @Getter @Setter @Accessors public class User { private String name; private Integer age; }
Generated code:
public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { = name; } public Integer getAge() { return age; } public void setAge(Integer age) { = age; } }
2.2 Enable chain calls (chain = true)
@Getter @Setter @Accessors(chain = true) public class User { private String name; private Integer age; }
Generated code:
public class User { private String name; private Integer age; public String getName() { return name; } public User setName(String name) { = name; return this; } public Integer getAge() { return age; } public User setAge(Integer age) { = age; return this; } }
Example of usage:
User user = new User().setName("Alice").setAge(25);
2.3 Enable the concise method name (fluent = true)
@Getter @Setter @Accessors(fluent = true) public class User { private String name; private Integer age; }
Generated code:
public class User { private String name; private Integer age; public String name() { return name; } public User name(String name) { = name; return this; } public Integer age() { return age; } public User age(Integer age) { = age; return this; } }
Example of usage:
User user = new User().name("Alice").age(25);
2.4 Ignore the field prefix (prefix)
@Getter @Setter @Accessors(prefix = "m_") public class User { private String m_name; private Integer m_age; }
Generated code:
public class User { private String m_name; private Integer m_age; public String getName() { return m_name; } public void setName(String name) { this.m_name = name; } public Integer getAge() { return m_age; } public void setAge(Integer age) { this.m_age = age; } }
Example of usage:
User user = new User(); ("Alice"); (25);
3. Combination use
Multiple parameters can be used in combination, for example:
@Getter @Setter @Accessors(chain = true, fluent = true, prefix = "m_") public class User { private String m_name; private Integer m_age; }
Generated code:
public class User { private String m_name; private Integer m_age; public String name() { return m_name; } public User name(String name) { this.m_name = name; return this; } public Integer age() { return m_age; } public User age(Integer age) { this.m_age = age; return this; } }
4. Things to note
-
and
@Data
Compatibility@Data
Already included@Getter
and@Setter
, so you can directly@Accessors
Use together. -
IDE Support
Make sure the IDE has the Lombok plugin installed, otherwise the generated code may not be recognized. -
Code readability
Although@Accessors
Code can be simplified, but overuse may lead to a decrease in code readability and requires trade-offs.
5. Summary
@Accessors
It is a powerful annotation in Lombok, through configurationchain
、fluent
andprefix
Parameters, you can flexibly customize the behavior of getter and setter methods. It's especially suitable for the needsChain callorSimple method nameThe scenario can significantly reduce boilerplate code and improve development efficiency.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.