大家在編寫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其他文章:
