1. 程式人生 > 其它 >SpringBoot - Lombok使用詳解5(@log、@Buinder、@SneakyThrows、@Synchronized)

SpringBoot - Lombok使用詳解5(@log、@Buinder、@SneakyThrows、@Synchronized)

七、Lombok註解詳解(4)

12,@log

(1)該註解用在類上,可以省去從日誌工廠生成日誌物件這一步,直接進行日誌記錄,具體註解根據日誌工具的不同而不同。不同的日誌註解總結如下(上面是註解,下面是實際作用):

我們也可以在註解中使用topic來指定生成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);

(2)下面是一個簡單的使用樣例:

// 使用註解
@Log
public class LogExample {
    public static void main(String... args) {
        log.error("Something's wrong here");
    }
}
 
// 不使用註解
public class LogExample {
    private static final java.util.logging.Logger log =
            java.util.logging.Logger.getLogger(LogExample.class.getName());
 
    public static void main(String... args) {
        log.error("Something's wrong here");
    }
}

13,@Buinder

(1)builder是現在比較推崇的一種構建值物件的方式。該描述符用於將類改造成builder(建造者)模式,用在類、方法或者建構函式上。
import lombok.Builder;
import lombok.Singular;
 
import java.util.Set;
 
@Builder
public class BuilderExample {
    private String name;
    private int age;
    @Singular
    private Set<String> occupations;
}

(2)上面相當於如下傳統的Java程式碼:

import java.util.Collection;
import java.util.Set;
 
public class BuilderExample {
    private String name;
    private int age;
    private Set<String> occupations;
 
    BuilderExample(String name, int age, Set<String> occupations) {
        this.name = name;
        this.age = age;
        this.occupations = occupations;
    }
 
    public static BuilderExampleBuilder builder() {
        return new BuilderExampleBuilder();
    }
 
    public static class BuilderExampleBuilder {
        private String name;
        private int age;
        private java.util.ArrayList<String> occupations;
 
        BuilderExampleBuilder() {
        }
 
        public BuilderExampleBuilder name(String name) {
            this.name = name;
            return this;
        }
 
        public BuilderExampleBuilder age(int age) {
            this.age = age;
            return this;
        }
 
        public BuilderExampleBuilder occupation(String occupation) {
            if (this.occupations == null) {
                this.occupations = new java.util.ArrayList<String>();
            }
 
            this.occupations.add(occupation);
            return this;
        }
 
        public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
            if (this.occupations == null) {
                this.occupations = new java.util.ArrayList<String>();
            }
 
            this.occupations.addAll(occupations);
            return this;
        }
 
        public BuilderExampleBuilder clearOccupations() {
            if (this.occupations != null) {
                this.occupations.clear();
            }
 
            return this;
        }
 
        public BuilderExample build() {
            //complicated switch statement to produce a compact properly sized immutable set omitted
            // go to https://projectlombok.org/features/Singular-snippet.html to see it.
            Set<String> occupations = ...;
            return new BuilderExample(name, age, occupations);
        }
 
        @java.lang.Override
        public String toString() {
            return "BuilderExample.BuilderExampleBuilder(name = " + this.name + ", age = "
                    + this.age + ", occupations = " + this.occupations + ")";
        }
    }
}

(3)下面是一個簡單的測試樣例:

BuilderExample be = BuilderExample.builder()
        .name("hangge")
        .age(123)
        .occupation("ABC")
        .occupation("DEF")
        .build();
 
return be.toString();

14,@SneakyThrows

(1)該註解用在方法上,可以將方法中的程式碼用try-catch語句包裹起來,捕獲異常並在catch中用Lombok.sneakyThrow(e)把異常丟擲。 (2)也可以使用@SneakyThrows(Exception.class)的形式指定丟擲哪種異常
// 使用註解
public class SneakyThrows implements Runnable {
    @SneakyThrows(UnsupportedEncodingException.class)
    public String utf8ToString(byte[] bytes) {
        return new String(bytes, "UTF-8");
    }
 
    @SneakyThrows
    public void run() {
        throw new Throwable();
    }
}
 
// 不使用註解
public class SneakyThrows implements Runnable {
    public String utf8ToString(byte[] bytes) {
        try{
            return new String(bytes, "UTF-8");
        }catch(UnsupportedEncodingException uee){
            throw Lombok.sneakyThrow(uee);
        }
    }
 
    public void run() {
        try{
            throw new Throwable();
        }catch(Throwable t){
            throw Lombok.sneakyThrow(t);
        }
    }
}

15,@Synchronized

該註解用在類方法或者例項方法上,效果和synchronized關鍵字相同,區別在於鎖物件不同。對於類方法和例項方法,它倆區別在於:

    • synchronized關鍵字的鎖物件分別是“類的class物件”和“this物件”
    • @Synchronized的鎖物件分別是“私有靜態 final 物件lock”和“私有final物件lock”。當然,也可以自己指定鎖物件。
// 使用註解
public class Synchronized {
    private final Object readLock = new Object();
 
    @Synchronized
    public static void hello() {
        System.out.println("world");
    }
 
    @Synchronized
    public int answerToLife() {
        return 42;
    }
 
    @Synchronized("readLock")
    public void foo() {
        System.out.println("bar");
    }
}
 
// 不使用註解
public class Synchronized {
    private static final Object $LOCK = new Object[0];
    private final Object $lock = new Object[0];
    private final Object readLock = new Object();
 
    public static void hello() {
        synchronized($LOCK) {
            System.out.println("world");
        }
    }
 
    public int answerToLife() {
        synchronized($lock) {
            return 42;
        }
    }
 
    public void foo() {
        synchronized(readLock) {
            System.out.println("bar");
        }
    }
}
早年同窗始相知,三載瞬逝情卻萌。年少不知愁滋味,猶讀紅豆生南國。別離方知相思苦,心田紅豆根以生。