在以往的对象模型编码时,我们需要写一大堆的get/set以及不同的构造函数等。Lombok为我们提供了一个非常好的插件形式。
在大多数的项目中,只需要使用到以下集中Annotation就足够了,如果需要查看更多的选项,请参考:传送门
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、雅安服务器托管、营销软件、网站建设、额济纳网站维护、网站推广。
@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
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 "";
}
分享文章:[Java开发利器Lombok]常用注解演示
网页URL:https://www.cdcxhl.com/article22/jicdjc.html
成都网站建设公司_创新互联,为您提供网站改版、商城网站、小程序开发、定制开发、电子商务、Google
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联