Back to Java

See Alse Lombok Annotations Support for VS Code

Lombok

Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。

1. Features

1.1. @ToString

No need to start a debugger to see your fields: Just let lombok generate a toString for you!

import lombok.ToString;

@ToString(callSuper=true, onlyExplicitlyIncluded = true)
public class MyBean {
  @ToString.Include
  public final String name;
  @ToString.Include
  public final float value;
}

1.2. @Data

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

1.3. @Builder

https://projectlombok.org/features/Builder

@Builder lets you automatically produce the code required to have your class be instantiable with code such as: Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();

1.4. @Slf4j

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CompositeServiceTest {
  public void testSomething() throws JsonProcessingException {
    Query query = queryService.getQuery("related-enterprises-summary");
    Object resp = compositeService.doSomething(query.getParams());
    log.info("resp={}", JSON.toJSONString(resp));
  }
}

1.5. @Accessors

https://projectlombok.org/features/experimental/Accessors A more fluent API for getters and setters.

@Accessors was introduced as experimental feature in lombok v0.11.0.

选项说明:

import lombok.experimental.Accessors;
@Accessors(chain = true)
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

...
User user = new User().setId(1L).setEmail("ab@c.c");

2. Reference

MainWiki: Lombok (last edited 2018-12-22 00:45:29 by twotwo)