Java lombok 好用annotation小筆記
Java lombok 好用annotation小筆記
建構子設定
@AllArgsConstructor(access = AccessLevel.PUBLIC)
AccessLevel --> 用來設定建構子的可視的等級 public private …
EX
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class RegisteredFlow {
private int flow;
private List<RunInfo> runInfos;
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public static class RunInfo {
private String runUid;
private State state;
private String endTag;
private String endLinkUid;
}
public static void main(String[] args){
List<RunInfo> l = new List<RunInfo>();
RegisteredFlow rf = new RegisteredFlow(123,l);
}
}
如果還要包含 空建構子, 可以再加入 @NoArgsConstructor
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@NoArgsConstructor
public class RegisteredFlow {
@AllArgsConstructor(access = AccessLevel.PUBLIC)
AccessLevel --> 用來設定建構子的可視的等級 public private …
EX
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class RegisteredFlow {
private int flow;
private List<RunInfo> runInfos;
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public static class RunInfo {
private String runUid;
private State state;
private String endTag;
private String endLinkUid;
}
public static void main(String[] args){
List<RunInfo> l = new List<RunInfo>();
RegisteredFlow rf = new RegisteredFlow(123,l);
}
}
如果還要包含 空建構子, 可以再加入 @NoArgsConstructor
@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@NoArgsConstructor
public class RegisteredFlow {
builder pattern
關鍵字 : @Builder
範例:
@Data
@Builder
public class Bundle {
private String runFlowUid;
private String flowUid;
private FlowRuner flowRuner;
public static void main(String[] args){
Bundle ans = BoxRuner.Bundle.builder().flowUid(model.getUid()).runFlowUid(uid).flowRuner(this).build();
}
}
@Data 設定某些欄位 geter seter 不要出現
- 關鍵字 :
@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
- ex:
@Data
public class Bundle {
private String runFlowUid;
private String flowUid;
@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
private FlowRuner flowRuner;
}
@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
@Data
public class Bundle {
private String runFlowUid;
private String flowUid;
@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
private FlowRuner flowRuner;
}
屬性自動產生geter 或 seter
關鍵字
@Getter
@Setter
- ex:
public class Bundle {
private String runFlowUid;
@Getter
@Setter
private String flowUid;
}
這樣不需要@Data 就可以對某個屬信產生 geter 或 seter
自動產生 Slf4j logger
關鍵字
@Slf4j
- ex:
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NodejsActionCall {
private void runCmd(Consumer<String> c, String... cmd) {
log.info("runCmd:" + cmd);
...
}
}
留言
張貼留言