Project Lombok - Getter, Setter and Constructor Example

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

Java Guides · Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours ... Skiptomaincontent Java JavaPrograms JavaforBeginners DSandAlgorithms JavaOOPSTutorial JavaforProfessionals JavaCollectionsTutorial JavaStringTutorial JavaExceptionsTutorial JavaRegexTutorial JavaMultithreading JavaGenericsTutorial JavaIOTutorial JDBCTutorial JavaFXTutorial SwingTutorial Java9Tutorial Java8Tutorial Java8DateTime Java8StreamAPI HeadFirstPatterns CoreJavaEEPatterns JavaBestPractices JavaEE JSPTutorial ServletTutorial JSPServletProjects JAXBTutorial JavaJSON-PTutorial MavenTutorial Library JavaAPIGuides JavaScriptAPIGuides ApacheVelocityTutorial KotlinTutorial EclipseTutorial JavaLangPackage JavaSQLPackage JavaUtilPackage JavaIOPackage JavaTimePackage JavaLangReflectPackage LombokTutorial JavaUtilityClasses REST RESTAPITutorial RestwithJavaTutorial JAX-RSTutorial JerseyRestTutorial JacksonJSONTutorial GoogleGSONTutorial JavaJSON-PTutorial JSON.simpleTutorial JavaXMLTutorial ApacheHttpClientTutorial JUnit JUnit5Tutorial JUnit4Tutorial JUnitExamples SpringBoot SpringBootTutorials[200+] SpringBootTestingTutorial SpringCoreTutorial SpringMVCTutorial SpringDataJPATutorial SpringFrameworkforBeginners SpringAOPTutorial SpringSecurityTutorial JavaThymeleafTutorial SpringBootAnnotations SpringBootFreeProjects FullStack AngularCRUDExamplewithSpringBoot SpringBoot+Angular12CRUDFullStack SpringBoot+Angular8CRUDFullStack SpringBoot+Angular10CRUDFullStack SpringBoot+ReactJSCRUDFullStack ReactJS(ReactHooks)+SpringBoot SpringBootThymeleafCRUDFullStack SpringBootUserRegistrationandLogin NodeJs+Express+MongoDBCRUD VueJS+SpringBootRESTAPITutorial YouTube SpringBootforBeginners SpringBootTutorialCourse 5SpringBootProjectsCourse SpringMVCTutorialCourse ValidationinSpringBoot SpringDataRESTCourse SpringBootRestAPICourse SpringBootHibernateCourse SpringBootWebApplication SpringBootAngularFullStack SpringBootReactFullStack ReactHooksSpringBootFullStack VueJSSpringBootFullStack JavaThymeleafTemplateCourse JavaCollectionsFramework MongoDBBeginnersCrashCourse Java8StreamAPITutorial Angular AngularCRUDOperations AllAngularTutorials Angular10Tutorial Angular9Tutorial JavaScriptTutorial TypeScriptTutorial LodashJSTutorial React ReactJSTutorial ReactJSTutorialforBeginners SpringBootReactCRUDFullStack SpringBootReact-FreeCourse Interview JavaArrayInterviewQ&A JavaTrickyCodingInterviewQ&A JavaStringInterviewQ&A JavaStringTrickyCodingQ&A Javamain()InterviewQ&A OOPSInterviewQ&A Java8InterviewQ&A JavaOOPSTrickyCodingQ&A JavaProgramsAskedinInterview JavaExceptionHandlingQ&A SpringBootInterviewQ&A Top10SpringMVCInterviewQ&A HibernateInterviewQ&A JPAInterviewQ&A DesignPatternsInterviewQ&A SpringCoreInterviewQ&A JavaStreamAPIInterviewQ&A JavaLambdaInterviewQ&A JavaFunctionalInterfaceInterviewQ&A Quiz JavaStringQuiz JavaArraysQuiz JavaLoopsQuiz JavaOOPSQuiz JavaOOPSQuiz-Part1 JavaOOPSQuiz-Part2 JavaExceptionHandlingQuiz JavaCollectionsQuiz JavaGenericsQuiz JavaMultithreadingQuiz JDBCQuiz JavaLambdaExpressionsQuiz JavaFunctionalInterfacesQuiz JavaStreamsAPIQuiz JavaDateTimeQuiz Java8Quiz Hibernate JavaPersistenceAPI HibernateORMTutorial HibernateDeveloperGuide SpringHibernateTutorials DB JavaMySQLTutorial JavaPostgreSQLTutorial JavaH2DatabaseTutorial JavaHSQLDBTutorial NoSQL:MongoDBTutorial JavaMongoDBTutorial Go Golang-Functions Golang-Operators Golang-ReadInput Golang-ReadWriteFile Golang-Array Golang-Slices Golang-Maps Golang-Structs Golang-JSON GolangBase64 Me AboutMe GitHub Courses Udemy YouTube ContactMe Advertise Resources MyUdemyBestsellerCourse- BuildingReal-TimeRESTAPIswithSpringBootandDeployonAWSCloud ProjectLombok-Getter,SetterandConstructorExample Author: RameshFadatare CoreJava ProjectLombok Getlink Facebook Twitter Pinterest Email OtherApps < org.projectlombok lombok 1.18.4 provided AddingtheLombokPlugininIDE(Eclipse) InordertoseeHerearetheinstallationstepsforWindows: Downloadedjarfrom https://projectlombok.org/download orusethejarwhichisdownloadedfromyourmavenbuild. Executecommandinterminal:java-jarlombok.jar Thiscommandwillopenwindowasshowninthepicturebelow,installandquittheinstallerandrestarteclipse. WithoutProjectLombok Let'sfirstlookatasampleofcodewhichhasaboilerplatecode.Let'screateaUserclasswithgettermethods,settermethods,andconstructors: packagenet.javaguides.lombok; /** * *@authorRameshFadatare * */ publicclassUser{ privatelongid; privateStringfirstName; privateStringlastName; privateintage; privateStringgender; publicUser(){} publicUser(longid,StringfirstName,StringlastName,intage,Stringgender){ super(); this.id=id; this.firstName=firstName; this.lastName=lastName; this.age=age; this.gender=gender; } publiclonggetId(){ returnid; } publicvoidsetId(longid){ this.id=id; } publicStringgetFirstName(){ returnfirstName; } publicvoidsetFirstName(StringfirstName){ this.firstName=firstName; } publicStringgetLastName(){ returnlastName; } publicvoidsetLastName(StringlastName){ this.lastName=lastName; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicStringgetGender(){ returngender; } publicvoidsetGender(Stringgender){ this.gender=gender; } } WithProjectLombok WithProjectLombok,wecanremovegettermethods,settermethods,defaultconstructorandparameterizedconstructorinUserclassbyaddingby@Getter,@Setter,@NoArgsConstructor,and@AllArgsConstructor: packagenet.javaguides.lombok.constructor; importlombok.AllArgsConstructor; importlombok.Getter; importlombok.NoArgsConstructor; importlombok.Setter; importlombok.ToString; @Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString publicclassUserLombokModel{ privatelongid; privateStringfirstName; privateStringlastName; privateintage; privateStringgender; } Testing AsweinstalledLombokpluginineclipsesowecantestProjectLombokannotations.Let'screateamain()methodandtestLombokannotations. packagenet.javaguides.lombok.constructor; publicclassLombokTest{ publicstaticvoidmain(String[]args){ //testconstructor UserLombokModellombokModel=newUserLombokModel(100,"Ramesh","Fadatare",28,"Male"); System.out.println(lombokModel.toString()); UserLombokModellombokMdl=newUserLombokModel(); //testgetterandsettermethods lombokMdl.setId(100L); lombokMdl.setFirstName("Ramesh"); lombokMdl.setLastName("Fadatare"); lombokMdl.setAge(28); lombokMdl.setGender("Male"); System.out.println(lombokMdl.getId()); System.out.println(lombokMdl.getFirstName()); System.out.println(lombokMdl.getLastName()); System.out.println(lombokMdl.getGender()); System.out.println(lombokMdl.getAge()); } } Notethatwehavetestedgettermethods,settermethods,defaultconstructorandparameterizedconstructorintheaboveexample. Output Wecandeclaregetterand settermethodsononlyforsomepropertiesbyusing@Getter,@Setterannotationforthoseproperties.Forexample,Ifweonlyneedgetter,settermethodforpropertyfirstName,lastNameinUserclass,wecandeclareasbelow: importlombok.AllArgsConstructor; importlombok.Getter; importlombok.NoArgsConstructor; importlombok.Setter; importlombok.ToString; @AllArgsConstructor @NoArgsConstructor @ToString publicclassUserLombokModel{ privatelongid; @Getter @Setter privateStringfirstName; @Getter @Setter privateStringlastName; privateintage; privateStringgender; } Reference https://projectlombok.org/features/all GitHubRepository YoucanviewthesourcecodeofthisarticleonmyGitHubrepositoryat https://github.com/RameshMF/project-lombok-tutorial RelatedProjectLombokArticles 1. ProjectLombok-@NonNullAnnotationExample 2. ProjectLombok-Getter,SetterandConstructorExample  3. ProjectLombok-toString(),equals()andhashCode()MethodExample 4. ProjectLombok-@DataAnnotationExample 5. ProjectLombok-BuilderPatternusing@BuilderAnnotation 6. ProjectLombok-Logging@Slf4jAnnotationExample 7. ProjectLombok-AutomaticResourceManagementusing@Cleanup 8. ProjectLombok-@SynchronizedAnnotationExample <



請為這篇文章評分?