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

作者: hangge 发布时间: 2019-10-10 浏览: 2218 次 编辑

七、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(建造者)模式,用在类、方法或者构造函数上。

package com.example.demo;
 
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 代码:

package com.example.demo;
 
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();

原文:SpringBoot - Lombok使用详解5(@log、@Buinder、@SneakyThrows、@Synchronized)

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");
        }
    }
}