In the past, when coding the object model, we need to write a lot of get/set and different constructors. Lombok provides us with a very good plug-in form.
In most projects, it's enough to use the following set of annotations. If you need to see more options, please refer to: Portal
- @Getter
- @Setter
- @ToString
-
@RequiredArgsConstructor
Constructor to generate final field
/** * java class */ @RequiredArgsConstructor class UserVO { private final Integer id; private final String name; private int age; } /** * Compiled code */ 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
Combinatorial annotation
/** * @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
Change the original assignment mode- Before use
- After use (builder mode, widely used in Feign source code)
- Before use
-
@Slf4j
lombok provides, equivalent to
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 ""; }