@Accessors(chain = true)

Lombok的@Accessors(chain=true)注解用于创建返回this而非void的setter方法,从而实现方法链。结合@Getter和@Setter使用,可以简化代码并提供更流畅的API。本文介绍了其用法示例、生成的代码以及额外选项,如自定义getter和setter的前缀。
摘要由CSDN通过智能技术生成

The @Accessors(chain = true) annotation is provided by Project Lombok to customize the generation of getter and setter methods for fields in a Java class. When you set chain = true, Lombok generates setter methods that return this (the current object) instead of void. This allows for a fluent API style, enabling method chaining.

Usage Example

Here is a simple example to demonstrate how @Accessors(chain = true) works in conjunction with Lombok's @Getter and @Setter annotations:

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class User {
    private int id;
    private String name;
    private String email;
}

public class Main {
    public static void main(String[] args) {
        User user = new User()
                        .setId(1)
                        .setName("John Doe")
                        .setEmail("john.doe@example.com");

        System.out.println(user);
    }
}

Explanation

  1. Annotations:

    • @Getter: Generates getter methods for all fields.
    • @Setter: Generates setter methods for all fields.
    • @Accessors(chain = true): Modifies the generated setter methods to return this instead of void.
  2. Method Chaining:

    • The setId(), setName(), and setEmail() methods return the current User object, allowing for chaining of setter method calls.
    • This results in more readable and concise code when setting multiple properties.

Generated Code

With @Accessors(chain = true), Lombok generates the following setter methods:

public User setId(int id) {
    this.id = id;
    return this;
}

public User setName(String name) {
    this.name = name;
    return this;
}

public User setEmail(String email) {
    this.email = email;
    return this;
}

Additional Options

The @Accessors annotation has other parameters you can use to customize the generated methods:

  • fluent: If set to true, Lombok generates getter and setter methods without the get and set prefixes. Default is false.
  • prefix: A list of strings to be removed from the field name when generating getter and setter methods.
Example with fluent and prefix
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true, fluent = true, prefix = "m")
public class User {
    private int mId;
    private String mName;
    private String mEmail;
}

public class Main {
    public static void main(String[] args) {
        User user = new User()
                        .id(1)
                        .name("John Doe")
                        .email("john.doe@example.com");

        System.out.println(user);
    }
}

In this example:

  • fluent = true: The generated getter and setter methods do not have the get and set prefixes. For example, setId(int id) becomes id(int id).
  • prefix = "m": The generated methods strip the prefix m from the field names. So, mId becomes id.

Summary

The @Accessors(chain = true) annotation from Lombok allows for method chaining by modifying setter methods to return the current object instead of void. This leads to a more fluent and readable API, especially when setting multiple properties. Additional customization options like fluent and prefix provide further control over the generated methods. This helps in writing clean and concise code while reducing boilerplate.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值