版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q343509740/article/details/80870723
六、使用:@Log
Lombok 为我们内置了各种日志组件的支持, 通常我们在SpringBoot项目开发中添加日志记录的功能, 而无论什么日志记录, 其添加时候都要先初始化, 然后才开始使用, 比如说如下的Log4j的初始化:
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
虽然说每个项目的初始化操作都比较简单,但是也架不住数量众多,所以说为了简化起见,lombok推出了@Log
注解,该注解就是帮助我们在日志中进行初始化操作的。其具体的使用方式为在所需要添加@Log的类名上添加如下注解:
@Slf4j
然后我们就可以像普通的已经初始化的日志程序一样, 通过log.
的方式进行调用了,例如打印 info信息:
log.info("");
注解API
CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
With Lombok
@Slf4j
public class Lesson29ApplicationTests {
@Test
public void testLombok(){
// 测试Getter/Setter
UserBean user = new UserBean();
user.setName("测试lombok");
user.setAge(10);
user.setAddress("测试地址");
System.out.println(user.toString());
log.info(user.toString());
}
}
Vanilla Java
public class Lesson29ApplicationTests {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Lesson29ApplicationTests.class);
@Test
public void testLombok(){
// 测试Getter/Setter
UserBean user = new UserBean();
user.setName("测试lombok");
user.setAge(10);
user.setAddress("测试地址");
System.out.println(user.toString());
log.info(user.toString());
}
}
七、使用:@NonNull
使用@NonNull 生成一个非空的检查
With Lombok
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}
Vanilla Java
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
}
八、使用:@SneakyThrows
@SneakyThrows的用法比较简单, 其实就是对异常的一个整理, 不处理, 直接扔掉. 减少到处写catch的不便利性. 比如在线程中,catch所有异常,再比如在一些不太可能发生异常的地方,但是你又必须cache checked exception的地方使用这个annotation会显得代码比较规整,易读。或许也会显得高大上一点吧. 但是这样的写法存在争议, 谨慎使用!
With Lombok
public class SneakyThrowsExample implements Runnable {
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
@SneakyThrows
public void run() {
throw new Throwable();
}
}
Vanilla Java
public class SneakyThrowsExample implements Runnable {
public String utf8ToString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Lombok.sneakyThrow(e);
}
}
public void run() {
try {
throw new Throwable();
} catch (Throwable t) {
throw Lombok.sneakyThrow(t);
}
}
}