IntelliJ IDEA reduce redundant codes using lombok

For POJO, we need to generate getter, setter methods for each of the fields, even though it can use the IDE to quickly generate for us. But if you need to modify the field names and field types, then you need to remove the re-generation, after all, there are some convenience. If you use lombok, through some simple annotations directly generate the code we need, can greatly improve the development experience.

  1. pom introduced dependence

     <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <optional>true</optional>
      </dependency>
  2. idea to download the plug

  3. lombok introduce common comment

    • @NonNull : When using @NonNull annotations modified field is set by set method If null, will throw NullPointerException
    • @Cleanup : Mainly used to modify the flow IO related classes, will be close () in a finally block the resource;
    • @Getter,@Setter : Generating getter for a field, the setter method, the marking to show that for all fields generated class
    • @ToString : Generate toString method, the default print all non-static fields
    • @EqualsAndHashCode : Generating equals and hashCode method
    • @NoArgsConstructor : No-argument constructor
    • @RequiredArgsConstructor : Generating constructor uninitialized final field and field labels used @NonNull
    • @AllArgsConstructor : Generating all fields constructor
    • @Data : Equivalent to the @ Getter, @ Setter, @ ToString, @ EqualsAndHashCode, @ RequiredArgsConstructor
    • @Value : The class will use the final modification, use @ ToString, @ EqualsAndHashCode, @ AllArgsConstructor, @ Getter
    • @Builder: Creating a static inner class, use the class can be used to create objects chained calls
      exist, such as the User object name, age field, User user = User.builder () name ( " name") .age (20) .build ( . )
    • @SneakyThrows : After tagging methods will be try catch throws an exception, an exception may need to catch an array of input value, the default catch Throwable
    • @Synchronized : Use synchronized ($ lock) in the method of labeling the code wrapping {}, $ lock for the new Object [0]
    • @Log,@CommonsLog,@JBossLog,@Log,@Log4j,@Log4j2,@Slf4j,@XSlf4j : Generate a current log object class, you can use the topic you want to get the name of the log, using the log ... use
  4. Custom Configuration

    Although lombok can quickly generate code for us, but there are some generated code still can not meet our needs at this time can be configured to solve the problem lombok.config

    Here are some common configuration

      lombok.getter.noIsPrefix=true(默认: false)  #lombok 默认对 boolean 类型字段生成的 get 方法使用 is 前缀, 通过此配置则使用 get 前缀
      lombok.accessors.chain=true(默认: false) #默认的 set 方法返回 void 设置为 true 返回调用对象本身
      lombok.accessors.fluent=true(默认: false) #如果设置为 true. get,set 方法将不带 get,set 前缀, 直接以字段名为方法名
      lombok.log.fieldName=logger(默认: log) #设置 log 类注解返回的字段名称

Note: In the IDEA, lombok.config Please placed in the main under src \ \ java directory, does not take effect in the src \ main \ resources will

** If you feel the article to help you, you can focus on the public micro-channel number] [color colorful encouragement **
![](https://img2018.cnblogs.com/blog/1821244/201910/1821244-20191014105209170-747095711.jpg)

Guess you like

Origin www.cnblogs.com/mrChangChang/p/11670273.html