在以往的對象模型編碼時,咱們須要寫一大堆的get/set以及不一樣的構造函數等。Lombok爲咱們提供了一個很是好的插件形式。
在大多數的項目中,只須要使用到如下集中Annotation就足夠了,若是須要查看更多的選項,請參考:傳送門html
@Getter
@Setter
@ToString
@RequiredArgsConstructor
生成final 字段的構造函數/** * java class */ @RequiredArgsConstructor class UserVO { private final Integer id; private final String name; private int age; } /** * 編譯後生成的代碼 */ class UserVO { private final Integer id; private final String name; private int age; public UserVO(Integer id, String name) { this.id = id; this.name = name; } }
@Data
組合註解/** * @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see EqualsAndHashCode * @see lombok.Value */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface Data { /** * ... */ String staticConstructor() default ""; }
@Builder
改變原有賦值模式@Slf4j
lombok 提供,等價於public static final Logger LOGGER = LoggerFactory.getLogger(UserCenterApplication.class);
/** * This annotation is valid for classes and enumerations.<br> * @see <a href="https://www.slf4j.org/api/org/slf4j/Logger.html">org.slf4j.Logger</a> * @see <a href="https://www.slf4j.org/api/org/slf4j/LoggerFactory.html#getLogger(java.lang.Class)">org.slf4j.LoggerFactory#getLogger(java.lang.Class)</a> * @see lombok.extern.apachecommons.CommonsLog @CommonsLog * @see lombok.extern.java.Log @Log * @see lombok.extern.log4j.Log4j @Log4j * @see lombok.extern.log4j.Log4j2 @Log4j2 * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface Slf4j { /** @return The category of the constructed Logger. By default, it will use the type where the annotation is placed. */ String topic() default ""; }