lombok注解相关

转载地址:https://blog.csdn.net/sunsfan/article/details/53542374

1.

@NonNUll

用在方法或者构造函数的参数前面,校验非空

2.

@ClearUp

用在需要关闭资源的变量前面,默认调用close() 方法

    public static void main(String[] args) throws IOException {
        @Cleanup FileInputStream in=new FileInputStream("/work/auth.txt");
        @Cleanup FileOutputStream ou=new FileOutputStream("/work/auth2.txt");

        byte[] b=new byte[1024];
        while (true){
            int i=in.read();
            if (i==-1){
                break;
            }
            ou.write(i);
        }

    }

这样就不用手动调用try-catch然后调用close方法关闭资源了

3.@Getter/@Setter

用在非静态成员变量上,自动添加get/set方法

4.@ToString

生成方法时只会使用类中的非静态非transient成员变量

为类添加toString方法

@ToString(of = {"param1"})
public class TestLombok {

    private String param1;

    private String param2;

    private String param3;

}

@ToString(of = {"param1"}) 生成的toString方法只有param1

@ToString(exclude= {"param1"}) 生成的toString方法不包含param1

5.@EqualsAndHashCode

生成方法时只会使用类中的非静态非transient成员变量

为类添加 equals()和hashCode()方法,

exclude和of属性同样适用

6.@NoArgsConstructor

添加无参构造方法

@AllArgsConstructor

添加包含所有参数的构造方法

@RequiredArgsConstructor

构造函数,字段只包括被@NonNull修饰和final的成员变量组成的构造函数

@RequiredArgConstructor(staticName = "testT")

staticName,产生一个名为"testT"的静态方法,返回 @RequiredArgConstructor 产生的构造函数返回的实例

7.@Data

添加如下:

  1. @Getter/@Setter
  2. @ToString
  3. @EqualsAndHashCode
  4. @RequiredArgsConstructor

8.@Value

和@Data效果类似,@Value不会产生set方法,同时会把每个成员变量设置成private final类型的

9.@Log

@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值