Lombok 註解簡潔說明

Lombok註解:

@Data:註解在類上,將類提供的所有屬性都添加get、set方法,並添加、equals、canEquals、hashCode、toString方法

@Setter:註解在類上,爲所有屬性添加set方法、註解在屬性上爲該屬性提供set方法

@Getter:註解在類上,爲所有的屬性添加get方法、註解在屬性上爲該屬性提供get方法

@NotNull:在參數中使用時,如果調用時傳了null值,就會拋出空指針異常

@Synchronized 用於方法,可以鎖定指定的對象,如果不指定,則默認創建一個對象鎖定

@Log作用於類,創建一個log屬性

@Builder:使用builder模式創建對象

@NoArgsConstructor:創建一個無參構造函數

@AllArgsConstructor:創建一個全參構造函數, 替代@Autowired構造注入,多個bean 注入時更加清晰

@Slf4j
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
   private final HystrixFallbackHandler hystrixFallbackHandler;
   private final ImageCodeHandler imageCodeHandler;
   
}


@Slf4j
@Configuration
public class RouterFunctionConfiguration {
   @Autowired
   private  HystrixFallbackHandler hystrixFallbackHandler;
   @Autowired
   private  ImageCodeHandler imageCodeHandler;
}

@ToString:創建一個toString方法

@Accessors(chain = true)使用鏈式設置屬性,set方法返回的是this對象。

@RequiredArgsConstructor:創建對象, 例: 在class上添加@RequiredArgsConstructor(staticName = “of”)會創建生成一個靜態方法

@UtilityClass:工具類再也不用定義static的方法了,直接就可以Class.Method 使用

@UtilityClass
public class Utility {

    public String getName() {
        return "name";
    }
}

public static void main(String[] args) {
    System.out.println(Utility.getName());
}

@ExtensionMethod:設置父類

@FieldDefaults:設置屬性的使用範圍,如private、public等,也可以設置屬性是否被final修飾。

@SneakyThrows

@SneakyThrows
private void checkCode(ServerHttpRequest request) {
   String code = request.getQueryParams().getFirst("code");

   if (StrUtil.isBlank(code)) {
   	throw new ValidateCodeException("驗證碼不能爲空");
   }

   redisTemplate.delete(key);
}


// 不使用就要加這個拋出
private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
   String code = request.getQueryParams().getFirst("code");

   if (StrUtil.isBlank(code)) {
   	throw new ValidateCodeException("驗證碼不能爲空");
   }
}

@EqualsAndHashCode:重寫equals和hashcode方法。

@Cleanup: 清理流對象,不用手動去關閉流

@Cleanup
OutputStream outStream = new FileOutputStream(new File("text.txt"));
@Cleanup
InputStream inStream = new FileInputStream(new File("text2.txt"));
byte[] b = new byte[65536];
while (true) {
   int r = inStream.read(b);
   if (r == -1) break;
   outStream.write(b, 0, r); 
}

歡迎關注博主公衆號:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章