Lombok @Getter and @Setter - Home | Java By Examples
文章推薦指數: 80 %
When we annotate a class with @Getter and @Setter, Lombok generates the getter and setter methods for all non-static fields. ... It has two fields, username, and ... Lombok@Getterand@Setter Libraries Lombok lombok 1.Overview Inthistutorial,we'lllookattheLombok@Getterand@Setterannotationstogenerategetterandsettermethodsautomatically. 2.Use@Getterand@SetteronClass Whenweannotateaclasswith@Getterand@Setter,Lombokgeneratesthegetterandsettermethodsforallnon-staticfields. We'llworkwiththeAccountclass: @Getter @Setter publicclassAccount{ privateStringusername; privateStringpassword; } Ithastwofields,username, andpassword. Also,we'reannotatingtheclasswiththe @Getterand@Setterannotations. Whenwecompileourcode,Lombokmustgenerategettersandsettersforusernameandpassword: publicclassAccount{ privateStringusername; privateStringpassword; publicStringgetUsername(){ returnthis.username; } publicStringgetPassword(){ returnthis.password; } publicvoidsetUsername(Stringusername){ this.username=username; } publicvoidsetPassword(Stringpassword){ this.password=password; } } Indeed,thegetterandsettermethodsarecreated. 3.Use@Getterand@SetteronFields Wecanalsoannotateinstancefieldswith@[email protected],@Gettergeneratesagettermethodand @Settergeneratesasettermethodfortheannotatedfield: publicclassAccount{ @Getter @Setter privateStringusername; @Getter @Setter privateStringpassword; } Insteadofplacingtheannotationsontotheclass,we'replacingthemontothefields.Nevertheless,thegeneratedmethodswillbeverysimilartothepreviousexample: publicclassAccount{ privateStringusername; privateStringpassword; publicStringgetUsername(){ returnthis.username; } publicStringgetPassword(){ returnthis.password; } publicvoidsetUsername(Stringusername){ this.username=username; } publicvoidsetPassword(Stringpassword){ this.password=password; } } 4.Configure@Getterand@Setterwith@Accessors Wecanfine-tunethebehaviorof @Getterand@Setterbyusingthe @Accessorsannotation. Forexample,wecanmakegetters/settersfluent,sothatgetName becomesname. WecanalsomakesetterschainablesothatsetName returnsthecurrent instanceinsteadofvoid. Let'sseetheusagewiththeAccountclass: @Getter @Setter @Accessors(fluent=true,chain=true) publicclassAccount{ privateStringusername; privateStringpassword; } Herewe'[email protected]'restatingthatgettersandsettersshouldbefluentandchainable. Whenwelookatthegeneratedcode: publicclassAccount{ privateStringusername; privateStringpassword; publicStringusername(){ returnthis.username; } publicStringpassword(){ returnthis.password; } publicAccountusername(Stringusername){ this.username=username; returnthis; } publicAccountpassword(Stringpassword){ this.password=password; returnthis; } } Wehavetheaccessormethods,butget/setprefixesaren'tthereanymore.Furthermore,settermethodsreturnthecurrentinstance,namelythis. 5.Summary Inthistutorial,we'velookedathowwecanusetheLombok @Getterand@Setterannotationstogenerateaccessorsmethods. Asalways,thesourcecodeisavailableonGithub. FurtherReadingonGuidetoLombok Lombok@ToString Lombok@EqualsAndHashCode Lombok@NonNull Lombok@Builder Lombok@AllArgsConstructor,@NoArgsConstructorand@RequiredArgsConstructor Lombok@Dataand@Value Lombok@SneakyThrows Lombok@Log4j,@Slf4jandOtherLogAnnotations
延伸文章資訊
- 1Omitting Getter or Setter in Lombok | Baeldung
Lombok provides two accessor annotations, @Getter and @Setter. We can annotate every field or sim...
- 2Introduction to Project Lombok | Baeldung
Java is a great language, but it can sometimes get too verbose for ... By adding the @Getter and ...
- 3Lombok @Getter and @Setter - Home | Java By Examples
When we annotate a class with @Getter and @Setter, Lombok generates the getter and setter methods...
- 4Day 08 - Spring Boot JPA 與Lombok 套件& DAO - iT 邦幫忙
可以聲明@Getter @Setter 為此類的屬性自動產生Get/ Set方法 ... 是官方提出的ORM規範,也是一個Java 應用程式的介面,利用標註Annotation 的方式或XML描...
- 5@Getter and @Setter - Project Lombok