关于Lombok不常见的使用方式

package com.example.cisum.utils;


import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;

import java.io.*;
import java.util.List;

@Slf4j//打印日志
@XSlf4j//打印日志2
@Log4j//打印日志3
@Log4j2//打印日志4
@CommonsLog//打印日志5
public class LombokDemo {

    @Setter
    @Getter
    @EqualsAndHashCode(of = {"name", "age"})//指定哪些字段参与
    @ToString(of = {"name", "age"})//指定哪些字段参与
    @NoArgsConstructor
    @AllArgsConstructor(access = AccessLevel.PRIVATE)//定义构造函数的修饰
    private static class Demo1 {
        private String name;
        private Integer age;
    }

    @Data//geter setter tostring equel..
    @Value
    @Accessors(chain = true, fluent = true)
    private static class Demo2 {
        private String name;
        private Integer age;
    }

    @Data
    @Accessors(chain = true, fluent = true)//fluent 省去getName setName。。
    private static class Demo3 {
        private String name;
        private Integer age;

        public static void main(String[] args) {
            Demo3 demo = new Demo3();
            demo.age(111);//fluent 省去getName setName。。
            demo.name("xxx");

            demo.name("xx").age(11);//chain  链式结构
            System.out.println(demo.age + "-" + demo.name);
        }
    }

    @Builder//构造者模式
    private static class Demo4 {
        private String name;
        private Integer age;
        @Singular("addList")
        private List<String> list;

        public static void main(String[] args) {
            Demo4 demo = Demo4.builder().name("xxx").age(222).addList("aa").addList("bbb").build();
            System.out.println(demo);
        }
    }

    @Synchronized//方法加锁
    private void method2() {

    }

    @SneakyThrows//try catch
    //Cleanup注解   替代close
    private static void copyFileUsingFileStreams(File source, File dest) {
        @Cleanup InputStream input = new FileInputStream(source);
        @Cleanup OutputStream output = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
        }
    }

    @Data//geter setter tostring equel..
    @Accessors(chain = true)
    @FieldNameConstants//获取属性变量名
    private static class Demo6 {
        private String name;
        private Integer age;

        public static void main(String[] args) {
            System.out.println(Fields.name);
            System.out.println(Fields.age);
        }
    }
}

 

posted @ 2022-12-04 10:36  蔡徐坤1987  阅读(60)  评论(0编辑  收藏  举报