Lombok Builder with Custom Setter – 带有自定义设定器的龙目建设者

最后修改: 2018年 10月 31日

1. Introduction


Project Lombok is a popular Java library to help reduce the amount of boilerplate code a developer needs to write.

Project Lombok是一个流行的Java库,可以帮助减少开发人员需要编写的模板代码的数量。

In this tutorial, we’ll take a look at how Lombok’s @Builder annotation works and how we can customize it for our specific needs.


2. Maven Dependency


Let’s start by adding the dependency to our pom.xml:



3. Lombok Builder Annotation


Before we look into customizing Lombok’s generated builder class, let’s do a quick recap of how the Lombok @Builder annotation works. We already have a full introduction into Lombok’s features.

在我们研究定制Lombok生成的构建器类之前,让我们快速回顾一下Lombok @Builder注解是如何工作的。我们已经有一个完整的介绍Lombok的功能。

The @Builder annotation can be used to automatically generate a builder for our class. For our example, we’ll use a messaging system where one user can send a message to another user. The message is either a simple text string or a File. Using Lombok, we can define our Message class as follows:


public class Message {
    private String sender;
    private String recipient;
    private String text;
    private File file;

@Data generates all the boilerplate that is normally associated with a simple POJO (Plain Old Java Object): getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode implementations, and a constructor.

@Data生成所有通常与简单POJO(Plain Old Java Object)相关的模板:所有字段的getters,所有非最终字段的setters,以及适当的toStringequalshashCode实现,和一个构造器。

Using the generated builder, we can now generate instances of our Message class:


Message message = Message.builder()
  .sender("[email protected]")
  .recipient("[email protected]")
  .text("How are you today?")

The @Builder annotation also supports default values for attributes but we won’t go into that now. It should be clear from this example that the @Builder annotation is quite powerful and can replace a lot of boilerplate code.

@Builder 注解还支持属性的默认值,但我们现在不会去讨论这个问题。从这个例子中应该可以看出,@Builder 注解是相当强大的,可以取代很多模板代码。

4. Customizing Lombok Builders


The previous section showed how we can use Lombok to generate a builder class. But there may be cases where the generated builder is not enough. In our example, we have a constraint that the message can only contain either text or a file. It cannot have both. Lombok doesn’t know that of course and the generated builder will happily allow us to get into that illegal state.


Luckily we can address this problem customizing the builder.


Customizing a Lombok builder is simple and straightforward: we write the parts of the builder that we want to customize and the Lombok @Builder annotation will simply not generate those parts. So in our example, that would be:

定制一个Lombok构建器是简单而直接的。我们写下我们想要定制的构建器的部分,Lombok @Builder 注解将简单地不生成这些部分。所以在我们的例子中,这将是。

public static class MessageBuilder {
    private String text;
    private File file;

    public MessageBuilder text(String text) {
        this.text = text;
        return this;

    public MessageBuilder file(File file) {
        this.file = file;
        return this;

    private void verifyTextOrFile() {
        if (text != null && file != null) {
            throw new IllegalStateException("Cannot send 'text' and 'file'.");

Please note that we did not have to declare the sender and recipient members, or the builder methods associated with them. Lombok will still generate those for us.


If we try to generate a Message instance with both text and a file with the following code:


Message message = Message.builder()
  .sender("[email protected]")
  .recipient("[email protected]")
  .text("How are you today?")
  .file(new File("/path/to/file"))

It will result in the following exception:


Exception in thread "main" java.lang.IllegalStateException: Cannot send 'text' and 'file'.

5. Conclusion


In this quick article, we looked at how to customize the Lombok builder.

在这篇快速文章中,我们看了如何定制Lombok builder。

As always, the code is available over on GitHub.