大家在编写springboot项目的过程中可能会接触到lombok这个插件,这个插件可以在编译时帮我生成很多代码。
1、@Data生成Getter和Setter代码,用于类名注释
2、@Getter 生成字段对应的getXXX方法
3、@Setter生成字段对应的setXXX(xxx yyy)方法
4、@Builder构造器设计模式生成个字段的设置属性方法,该字段一般用于一些类参数可以选,可单选,可多选的环境中
5、@Slf4j使用Slf4j中门面模式,适配底层日志框架,slf4j和底层日志框架协调记录日志
6、@NoArgsConstructor生成类的默认构造函数
7、@AllArgsConstructor生成类所有字段的构造函数,常常和@Builder同时出现
8、@ToString,笔者不常用这个
9、var 兼容ECMA规范,可以编译时自动推断数类型的注解
代码如下
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import lombok.extern.slf4j.Slf4j; import lombok.var; @Data @Getter @Setter @Slf4j @NoArgsConstructor @AllArgsConstructor @Builder @ToString public class LombokClass { @Setter private Integer id; @Setter @Getter private String name; public void test() { var t ="s"; } }
生成代码:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LombokClass { private static final Logger log; private Integer id; private String name; public void test() { final String t = "s"; } public static LombokClassBuilder builder() { return new LombokClassBuilder(); } @Override public boolean equals(final Object o) { if (o == this) { return true; } if (!(o instanceof LombokClass)) { return false; } final LombokClass other = (LombokClass)o; if (!other.canEqual(this)) { return false; } final Object this$id = this.getId(); final Object other$id = other.getId(); Label_0065: { if (this$id == null) { if (other$id == null) { break Label_0065; } } else if (this$id.equals(other$id)) { break Label_0065; } return false; } final Object this$name = this.getName(); final Object other$name = other.getName(); if (this$name == null) { if (other$name == null) { return true; } } else if (this$name.equals(other$name)) { return true; } return false; } protected boolean canEqual(final Object other) { return other instanceof LombokClass; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final Object $id = this.getId(); result = result * 59 + (($id == null) ? 43 : $id.hashCode()); final Object $name = this.getName(); result = result * 59 + (($name == null) ? 43 : $name.hashCode()); return result; } public Integer getId() { return this.id; } public LombokClass() { } public LombokClass(final Integer id, final String name) { this.id = id; this.name = name; } @Override public String toString() { return "LombokClass(id=" + this.getId() + ", name=" + this.getName() + ")"; } public void setId(final Integer id) { this.id = id; } public void setName(final String name) { this.name = name; } public String getName() { return this.name; } static { log = LoggerFactory.getLogger((Class)LombokClass.class); } public static class LombokClassBuilder { private Integer id; private String name; LombokClassBuilder() { } public LombokClassBuilder id(final Integer id) { this.id = id; return this; } public LombokClassBuilder name(final String name) { this.name = name; return this; } public LombokClass build() { return new LombokClass(this.id, this.name); } @Override public String toString() { return "LombokClass.LombokClassBuilder(id=" + this.id + ", name=" + this.name + ")"; } } }
本文要讲解的重点是@Slf4j相关的使用,使用该注解默认生成代码如下
import org.slf4j.Logger; private static final Logger log = org.slf4j.LoggerFactory.getLogger(当前类名.class);
log有以下几个方法info,trace,debug,error,warn及对应的重载方法。
追踪了一下代码,内部会使用StringBuilder这个线程安全类处理,StringBuilder比String速度比较快,他不会创建多个String对象。
以上几个方法常用格式化代码如下(使用{}作为占位符):
来自:https://examples.javacodegeeks.com/enterprise-java/slf4j/slf4j-format-string-example/
package com.javacodegeeks.slf4.formatting; import java.lang.invoke.MethodHandles; import java.text.MessageFormat; import java.util.Calendar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Substituting Parameters! * */ public class Slf4jSusbstitutionExample { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static void main( String[] args ) { String user = "john"; String application = "gateway"; // Crafting a message without substitution. // Not a good idea as the String concatenation and evaluation will happen irrespective of whether // logging level is permissible or not to be logged. LOGGER.info("Bad experience for user " + user + " at time " + Calendar.getInstance().getTime()); // Substitution with one formatting anchor and one argument LOGGER.info("Bad experience for user {}", user); // If you happen to forget to provide a substituting object LOGGER.info("Bad experience for user {}"); // Substitution with two formatting anchors and two arguments LOGGER.info("Bad experience for user {} at time {}", user, Calendar.getInstance().getTime()); // Substitution with three formatting anchors and three arguments LOGGER.info("Bad experience for user {} at time {} while accessing {}", user, Calendar.getInstance().getTime(), application); // Escaping formatting anchor LOGGER.info("ERROR CODE \{}; Bad experience for user {} at time {}", user, Calendar.getInstance().getTime()); // Formatting anchor with data inside; no problem LOGGER.info("ERROR CODE {22}; Bad experience for user {} at time {}", user, Calendar.getInstance().getTime()); // Crafting a message with Java's own MessageFormatter. // Not a good idea as per SLF4J's documentation. // 1. SLF4J's implementation is 10 times faster than that of MessageFormat. // 2. Moreover to make sure that the evaluation happens only if that particular logging // level is allowed, you need to do a check. if(LOGGER.isInfoEnabled()) { String message = MessageFormat.format("Bad experience for user {0} at time {1} while accessing {2}", user, Calendar.getInstance().getTime(), application); LOGGER.info(message); } } }
输出结果:
2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user john at time Thu Apr 20 20:25:42 IST 2017 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user john 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user {} 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user john at time Thu Apr 20 20:25:42 IST 2017 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user john at time Thu Apr 20 20:25:42 IST 2017 while accessing gateway 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - ERROR CODE {}; Bad experience for user john at time Thu Apr 20 20:25:42 IST 2017 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - ERROR CODE {22}; Bad experience for user john at time Thu Apr 20 20:25:42 IST 2017 2017-04-20 20:25:42 INFO Slf4jSusbstitutionExample - Bad experience for user john at time 4/20/17 8:25 PM while accessing gateway
本博客lombok其他文章: