lombok常见注解概述,部分简单注解示例

编程入门 行业动态 更新时间:2024-10-27 14:32:54

Lombok 常用的注解

注解描述@Getter / @Setter可以作用在类上和属性上,放在类上,会对所有的非静态(non-static)属性生成Getter/Setter方法,放在属性上,会对该属性生成Getter/Setter方法。并可以使用该注解中的AessLevel属性来指定Getter/Setter方法的访问级别。@ToString生成toString方法,默认情况下,会输出类名、所有属性,属性会按照顺序输出,以逗号分割。可以使用该注解中的exclude属性来指定生成的toSpring方法不包含对象中的哪些字段,或者使用of属性来指定生成的toSpring方法只包含对象中的哪些字段@EqualsAndHashCode默认情况下,会使用所有非瞬态(non-transient)和非静态(non-static)字段来生成equals和hascode方法,也可以使用exclude或of属性。@NoArgsConstructor生成无参构造器@RequiredArgsConstructor会生成一个包含标识了@NonNull注解的变量的构造方法。生成的构造方法是private,如果想要对外提供使用的话,可以使用staticName选项生成一个static方法。@AllArgsConstructor生成全参构造器,当我们需要重载多个构造器的时候,Lombok就无能为力了。@Slf4j该注解是用来解决不用每次都写 private final Logger logger = LoggerFactory.getLogger(XXX.class); 这句代码的。使用的日志框架是LogBack@Log4j该注解也是用来解决不用每次都写日志对象声明语句的,从字面上也可以看出,使用的日志框架是log4j@Data该注解是 @ToString、@EqualsAndHashCode注解,和所有属性的@Getter注解, 以及所有non-final属性的@Setter注解的组合,通常情况下,我们使用这个注解就足够了。 以上只列出了部分常用注解,更多注解的使用方式,请参考 官网关于注解的文档
package .huawei.guanggao.pojo;import lombok.*;import java.io.Serializable;
import java.util.Date;@Getter
@Setter//全参构造函数
@AllArgsConstructor
//无参构造函数
@NoArgsConstructor//不包括某个字段
//@ToString(exclude = {"pwd","phone"})
//只输出某个字段
//@ToString(of = {"pwd","phone"})
@ToString//作用于类,覆盖默认的equals和hashCode, 作用于全部属性
@EqualsAndHashCode//不包括某个属性
@EqualsAndHashCode(exclude = {"age"})//只输出某个属性
@EqualsAndHashCode(of = {"name"})//构造者模式 (Goods goods = Goods.builder().name("硒鼓").id(3).build();)
@Builder
public class Goods implements Serializable {private static final long serialVersionUID = -1939317434177299884L;/*** final 只会生成get*/private final String biaoti="史蒂芬孙";/*** 下面两个静态成员变量不会生成set/get方法*/static Date createTime = new Date();private static final String address = "广东省广州市";private Integer id;private String name;/*** 不想生成 get ,set 方法*/@Getter(value = AessLevel.NONE)@Setter(value = AessLevel.NONE)private String pwd;/*** 控制访问权限*/@Getter(value =AessLevel.PROTECTED)private String phone;private Integer stock;}

@Data, 组合注解,相当于以下注解的集合 (注意:全参构造用不了,可以用@Builder的方式)
@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor

@Slf4j或@Log4j注解,日志使用
log.info("日志");

 
编译后的文件,有些代码省略了,大概就是这个意思

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower depiler)
//package .huawei.guanggao.pojo;import java.io.Serializable;
import java.util.Date;public class Goods implements Serializable {private static final long serialVersionUID = -1939317434177299884L;private final String biaoti = "史蒂芬孙";static Date createTime = new Date();private static final String address = "广东省广州市";private Integer id;private String name;private String pwd;private String phone;private Integer stock;public static Goods.GoodsBuilder builder() {return new Goods.GoodsBuilder();}public String getBiaoti() {this.getClass();return "史蒂芬孙";}public Integer getId() {return this.id;}public String getName() {return this.name;}public Integer getStock() {return this.stock;}public void setId(final Integer id) {this.id = id;}public void setName(final String name) {this.name = name;}public void setPhone(final String phone) {this.phone = phone;}public void setStock(final Integer stock) {this.stock = stock;}public Goods(final Integer id, final String name, final String pwd, final String phone, final Integer stock) {this.id = id;this.name = name;this.pwd = pwd;this.phone = phone;this.stock = stock;}public Goods() {}public String toString() {return "Goods(biaoti=" + this.getBiaoti() + ", id=" + this.getId() + ", name=" + this.getName() + ", pwd=" + this.pwd + ", phone=" + this.getPhone() + ", stock=" + this.getStock() + ")";}protected String getPhone() {return this.phone;}public static class GoodsBuilder {private Integer id;private String name;private String pwd;private String phone;private Integer stock;GoodsBuilder() {}public Goods.GoodsBuilder id(final Integer id) {this.id = id;return this;}public Goods.GoodsBuilder name(final String name) {this.name = name;return this;}public Goods.GoodsBuilder pwd(final String pwd) {this.pwd = pwd;return this;}public Goods.GoodsBuilder phone(final String phone) {this.phone = phone;return this;}public Goods.GoodsBuilder stock(final Integer stock) {this.stock = stock;return this;}public Goods build() {return new Goods(this.id, this.name, this.pwd, this.phone, this.stock);}public String toString() {return "Goods.GoodsBuilder(id=" + this.id + ", name=" + this.name + ", pwd=" + this.pwd + ", phone=" + this.phone + ", stock=" + this.stock + ")";}}
}

 

更多推荐

注解,示例,常见,简单,lombok

本文发布于:2023-05-31 12:02:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注解   示例   常见   简单   lombok

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!