java @Lombok the java code becomes simple, fast

Explanation

Official website address
is all in English do not understand? In short Lombok, annotations can be used in the project, such as to save you all: the object's constructor, get equals () method, the attribute () / set () methods, etc., no technical content of these codes all do not write, Lombok seal the deal all, all automatically generated to help you!

When using Lombok, IDE may appear an error, the format is not standardized. This time you need to install the plug-Lombok. The latest idea in general or automatically prompt you to install.

Then we started!

1.pom

Click here to select your favorite version in the central warehouse!

2. Notes

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {

    private String id;

    private String realName;

    private String gender;

    private String age;

    private String email;

    private String address;

    private String nickName;

    private String password;

    private String account;

}

(1)@Data

@Data annotation on the class, setter / getter, equals, canEqual, hashCode, toString method automatically generates classes for all properties, such as the final attribute, no attribute setter method for the generation.

(2)@Getter/@Setter

@ Getter / @ Setter annotation class may be automatically generated Getter / Setter method corresponding properties

(3)@NonNull

The annotation used on a property or constructor, Lombok is non-empty generates a statement can be used to verify the parameters, can help prevent a null pointer.

    @NotNull
    private String id;

    public User(@NotNull String id) {
        this.id = id;
    }

(4)@Cleanup

The notes can help us to automatically call close () method, greatly simplifies the code.

        @Cleanup FileInputStream inputStream = new FileInputStream("/asas/sasa/as");
        @Cleanup FileOutputStream outputStream = new FileOutputStream("/sasa/sa/ds");

(5)@EqualsAndHashCode

By default, it will use all non-static (non-static) and non-transient (non-transient) attribute is generated and equals hasCode, can also exclude annotation to exclude some properties.

(6)@ToString

Use @ToString annotations, Lombok is generated a toString () method, by default, will output class names, all attributes (attribute defines the order will follow), divided by a comma. By includeFieldNames parameter set to true, you can clear the output of toString () property

(7)@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

No argument constructor, some parameters configured, full-argument constructor. Lombok not achieve more argument constructor overload.

(8)@Builder

User user = User.builder().age("22").account("asdfasd").gender("asdfasf").build();

(9)@SneakyThrows

Implicit thrown

    @Test
    @SneakyThrows
    public void test() {
        User user = User.builder().age("22").account("asdfasd").gender("asdfasf").build();
        @Cleanup FileInputStream inputStream = new FileInputStream("/asas/sasa/as");
        @Cleanup FileOutputStream outputStream = new FileOutputStream("/sasa/sa/ds");
    }
Published 29 original articles · won praise 0 · Views 353

Guess you like

Origin blog.csdn.net/qq_43399077/article/details/104068560