Lombok @Getter @Setter and lazy getters examples
文章推薦指數: 80 %
This lets you override the behavior of a @Getter , @Setter or @Data annotation on a class. Lobmoked User.java. @Getter.
JavaNews
CoreJava
JUnit5
ORM
JPAwithHibernate
Lombok
ContactUs
Search
javabydeveloperJavaTutorials
Signin
Welcome!Logintoyouraccount
yourusername
yourpassword
Forgotyourpassword?Gethelp
PrivacyPolicy
Passwordrecovery
Recoveryourpassword
youremail
Apasswordwillbee-mailedtoyou.
javabydeveloperJavaTutorials
JavaNews
CoreJava
JUnit5
ORM
JPAwithHibernate
Lombok
ContactUs
Search
Signin/Join
Facebook
Twitter
Pinterest
WhatsApp
Linkedin
Email
Telegram
reportthisad
HomeLombokLombok@Getter@Setterandlazygettersexamples
Lombok@Getter@Setterandlazygettersexamples
BySatishVarma
August11,2020
CompleteguidetoexplaintheusageofLombok@Getter,@Setterannotationsandgeneratinglazygetterswithseveralexamples.
TableofContent
[hide]1.AnnotatingFieldswith@[email protected]@Getterand@Setter3.Changingaccesslevelforgeneratedgettersandsetters4.ExcludeOrdisablegeneratinggetters/setters5.ReturnOptionalforGetter6.Getterforbooleanfileds7.ReturnthisforSetters8.GenerateLazyGetter9.PutAnnotationsonLombokgeneratedConstructors,methodsandparameters10.Conclusion
1.AnnotatingFieldswith@Getterand@Setter
AnnotateanyfieldwithLombok’s@Getterand@Settertogeneratethedefaultgetter/setterautomaticallybyLombok.Thegeneratedgetter/settermethodwillbe public bydefault.Let’sseethefollowingsidebysideexamples,therightsideexampleisthedelombokedjavacodefortheleftsidejavacode.
MakesureyoualreadyinstalledLomboksetupforyourIDE.ToSetupinEclipseorinSpringToolSuiterefertoourLombokMavenexamplesetupwithEclipse.
LobmokedUser.java
publicclassUser{
@Getter
privateLongid;
@Setter
privateStringusername;
@Getter@Setter
privateStringemail;
@Getter@Setter
privateLocalDatelastUpdated;
}
DelombokedUser.Java
//GeneratedbydelombokatTueMar3116:03:35SGT2020
packagecom.javabydeveloper.lombok.settergetter;
importjava.time.LocalDate;
publicclassUser{
privateLongid;
privateStringusername;
privateStringemail;
privateLocalDatelastUpdated;
publicStringgetUsername(){
returnthis.username;
}
publicvoidsetId(finalLongid){
this.id=id;
}
publicLonggetId(){
returnthis.id;
}
publicvoidsetUsername(finalStringusername){
this.username=username;
}
publicStringgetEmail(){
returnthis.email;
}
publicvoidsetEmail(finalStringemail){
this.email=email;
}
publicLocalDategetLastUpdated(){
returnthis.lastUpdated;
}
publicvoidsetLastUpdated(finalLocalDatelastUpdated){
this.lastUpdated=lastUpdated;
}
}
2.AnnotatingClasswith@Getterand@Setter
Youcanalsoputa @Getter and/or @Setter annotationonaclass.Inthiscase
gettersandsetterswillbegeneratedforallnon-staticfields.Nosettersandgettersgenerateforstaticfields.Nosetterswillbegeneratedforfinalfields.
LobmokedUser.java
@Getter
@Setter
publicclassUser{
privateLongid;
privateStringusername;
privateLocalDatelastUpdated;
//nogettersettergeneratedforstaticfields
publicstaticintmaxAge=40;
//onlygetterswillbegeneratedforfinalfields
publicfinalintminAge=20;
}
DelombokedUser.Java
publicclassUser{
privateLongid;
privateStringusername;
privateLocalDatelastUpdated;
//nogettersettergeneratedforstaticfields
publicstaticintmaxAge=40;
//onlygetterswillbegeneratedforfinalfields
publicfinalintminAge=20;
publicLonggetId(){
returnthis.id;
}
publicStringgetUsername(){
returnthis.username;
}
publicLocalDategetLastUpdated(){
returnthis.lastUpdated;
}
publicintgetMinAge(){
returnthis.minAge;
}
publicvoidsetId(finalLongid){
this.id=id;
}
publicvoidsetUsername(finalStringusername){
this.username=username;
}
publicvoidsetLastUpdated(finalLocalDatelastUpdated){
this.lastUpdated=lastUpdated;
}
}
3.Changingaccesslevelforgeneratedgettersandsetters
Bydefaultgeneratedgetter/settermethodwillbe public.Tochangeaccessmodifierofgeneratedgettersandsetters,youneedtospecifyAccessLevelasshowninthefollowingexample.Thelegalaccesslevelsare PUBLIC, PROTECTED, PACKAGE,and PRIVATE.
LobmokedUser.java
@Getter
@Setter
publicclassUser{
@Setter(AccessLevel.PRIVATE)
privateLongid;
@Getter(AccessLevel.PROTECTED)
privateStringusername;
@Getter(AccessLevel.PACKAGE)
privateStringemail;
@Getter(AccessLevel.PUBLIC)
@Setter(AccessLevel.PRIVATE)
privateLocalDatelastUpdated;
}
DelombokedUser.Java
publicclassUser{
privateLongid;
privateStringusername;
privateStringemail;
privateLocalDatelastUpdated;
publicLonggetId(){
returnthis.id;
}
publicvoidsetUsername(finalStringusername){
this.username=username;
}
publicvoidsetEmail(finalStringemail){
this.email=email;
}
privatevoidsetId(finalLongid){
this.id=id;
}
protectedStringgetUsername(){
returnthis.username;
}
StringgetEmail(){
returnthis.email;
}
publicLocalDategetLastUpdated(){
returnthis.lastUpdated;
}
privatevoidsetLastUpdated(finalLocalDatelastUpdated){
this.lastUpdated=lastUpdated;
}
}
4.ExcludeOrdisablegeneratinggetters/setters
Youcanalwaysmanuallydisablegetter/settergenerationforanyfieldbyusingthespecial AccessLevel.NONE accesslevel.Thisletsyouoverridethebehaviorofa @Getter, @Setter or @Data annotationonaclass.
LobmokedUser.java
@Getter
@Setter
publicclassUser{
@Setter(AccessLevel.NONE)
privateLongid;
@Getter(AccessLevel.NONE)
privateStringusername;
privateStringemail;
privateLocalDatelastUpdated;
}
DelombokedUser.Java
publicclassUser{
privateLongid;
privateStringusername;
privateStringemail;
privateLocalDatelastUpdated;
publicLonggetId(){
returnthis.id;
}
publicStringgetEmail(){
returnthis.email;
}
publicLocalDategetLastUpdated(){
returnthis.lastUpdated;
}
publicvoidsetUsername(finalStringusername){
this.username=username;
}
publicvoidsetEmail(finalStringemail){
this.email=email;
}
publicvoidsetLastUpdated(finalLocalDatelastUpdated){
this.lastUpdated=lastUpdated;
}
}
5.ReturnOptionalforGetter
SometimesyoumaywanttoreturnOptionalforyouroneofgettermethod,butyoudon’twanttogeneratesettertosetOptionalforthesamefield.InfollowingexampleLombokwon’tgenerategettermethod,writingyourowngetterimplementationforcestoLomboknottogenerateit’sown.
LobmokedUser.java
@Getter
@Setter
publicclassUser{
privateLongid;
privateStringusername;
privateStringemail;
publicOptional
延伸文章資訊
- 1How do you override a Lombok getter? - QuickAdviser
You can always manually disable getter/setter generation for any field by using the special Acces...
- 2Lombok @Getter @Setter and lazy getters examples
This lets you override the behavior of a @Getter , @Setter or @Data annotation on a class. Lobmok...
- 3How to override Lombok Setter methods - Java
I'm using lombok in my project and generation Setters and Getters using @Setters and @Getters ann...
- 4史上最全面的Lombok 註解介紹,Look 一下吧!
public class GetterSetterExample { @Getter @Setter private int age = 10; @Setter(AccessLevel.PROT...
- 5@Getter and @Setter - Project Lombok
This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class. To pu...