Lombok @Getter and @Setter - Home | Java By Examples

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?